0

I'm using maatwebsite/excel package and I would like to dynamically pass different file type as a second parameter.
See function here

Below is the variable:

$fileType = $request->input('fileType', 'xlsx');
$writerType = Excel::$fileType;

But I get the error:

Access to undeclared static property: Maatwebsite\Excel\Excel::$fileType

I try to use curly brackets but doesn't work:

Excel::${"fileType"};

How do I pass the variable? Thanks!

2 Answers2

0
 return Excel::create('PatientList',function($excel) use ($variable){

            $excel->sheet('List',function($sheet) use ($variable){
                $sheet->fromArray($variable);
            });

        })->download($type);

use like this when you download the file.pass $type from your view.

Ankita Mehta
  • 442
  • 3
  • 16
0

You passed variable correctly. But your the problem is that Maatwebsite\Excel\Excel contains constant XLSX and it's not a property. Constants can be accessed staticly Excel::XLSX or dynamicly using constant function:

     $fileType = $request->input('fileType', 'xlsx');
     $writerType = constant('Excel::' . strtoupper($fileType));
  • It will get `Couldn't find constant Excel::XLSX`. So I use full namespace: `$writerType = constant('\\Maatwebsite\\Excel\\Excel::' . strtoupper($fileType));` It works! Thanks. – crownhsueh Oct 17 '18 at 02:03