[英]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. 我正在使用maatwebsite / excel软件包,并且希望动态传递不同的文件类型作为第二个参数。
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
访问未声明的静态属性:Maatwebsite \\ Excel \\ Excel :: $ fileType
I try to use curly brackets but doesn't work: 我尝试使用大括号,但不起作用:
Excel::${"fileType"};
How do I pass the variable? 如何传递变量? Thanks!
谢谢!
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. 从您的视图下载file.pass
$type
时,请像这样使用。
You passed variable correctly. 您正确传递了变量。 But your the problem is that Maatwebsite\\Excel\\Excel contains constant XLSX and it's not a property.
但是您的问题是Maatwebsite \\ Excel \\ Excel包含常量XLSX,它不是属性。 Constants can be accessed staticly Excel::XLSX or dynamicly using constant function:
常量可以通过Excel :: XLSX静态访问,也可以使用常量函数动态访问:
$fileType = $request->input('fileType', 'xlsx');
$writerType = constant('Excel::' . strtoupper($fileType));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.