Search code examples
phplaravelmaatwebsite-exceldynamic-variableslaravel-excel

How to call PHP property from string stored in a Variable


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!


Solution

  • 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));