Search code examples
phplaravellaravel-8maatwebsite-excel

how to make Select function used in model dynamically select the column to export in excel in Laravel using Maatwebsite


I am working in the project using Laravel 8 and waatwebsite(to work with excel). I have one Table name Docs in my database which have 20 columns value. I dont Know how to make the select() function dynamical so that i can pass the column name from controller to extract the query and dump the column i need for other download.

Here, I have download other data in excel with different column rather then Lc_no, cur, value_date.

Also, How to set parameter of constructor string $search,string $from, string $to to null if not passed through controller.

Model

    class UserExport implements FromQuery
    {
        use Exportable;
        /**
        * @return \Illuminate\Support\Collection
        */
    
    use Exportable;
    
    
    public function __construct(string $search,string $from, string $to)
        {
            
            $this->from = $from;
            $this->to = $to;
            $this->search = $search;
            
        }
    
        public function query()
        {
    
            return Doc::query()->wherebetween($this->search, [$this->from,$this->to])->select('lc_no', 'cur', 'value_date');
        }

Controller

class ExportController extends Controller {

        function exports(){
            //return Excel::download(new UserExport, 'users.xlsx');
            return (new UserExport)->download('docs.xlsx');
        }
    
    
        function treasury_dump($from,$to)
        {
            $search = 'value_date';
                return (new UserExport($search,$from,$to))->download('treasury.xlsx');
    
    
        }

Solution

  • You can pass array as parameter instead of multiple parameter

    public function __construct($params)
    {
                
        $this->params = $params;
                
    }
        
    public function query()
    {
        
        return Doc::query()->wherebetween($this->params['search'], [$this->params['from'],$this->params['to']])->select($this->params['columns']);
    }
    

    in your controller

    $params=[
    'search'=>'asfsa',
    'from'=>'',
    'to'=>'',
    'columns'=>['lc_no', 'cur', 'value_date']
    ];
    
    return (new UserExport($params))->download('treasury.xlsx');    
    

    But make sure if you pass null for search ,from and to query will fail so better check before executing query like below

    Doc::query()->where(function($query)use(){
    if(isset($this->params['search'])&&!empty($this->params['search'])&&isset($this->params['from'])&&!empty($this->params['from'])&&isset($this->params['to'])&&!empty($this->params['to'])){
    $query->wherebetween($this->params['search'], [$this->params['from'],$this->params['to']]);
    }
    
    )->select($this->params['columns']);
    

    Updated

      Doc::query()->where(function($query) {
            if (isset($this->params['search']) && !empty($this->params['search']) && isset($this->params['from']) && !empty($this->params['from']) && isset($this->params['to']) && !empty($this->params['to'])) {
                $query->wherebetween($this->params['search'], [$this->params['from'], $this->params['to']]);
            }
        }
        )->select($this->params['columns']);