I'm doing an excel file export from an eloquent model using Laravel and Maatwebsite/Laravel-Excel.
Here's what I have
public function export(Request $request)
{
$data = $this->getData($param1)->get();
$export = Excel::create('example' . date('m-d-Y-G-His'), function ($excel) {
$excel->setDescription('example');
});
$export->sheet('Example', function ($sheet) {
$sheet->row(1, array(
'row1',
'row2',
'row3'
));
$i = 2;
foreach ($data as $d) {
$sheet->row($i, array(
$d->id,
$d->name, $d->date,
));
}
});
}
And I get Undefined variable: data
I understand that is a scope problem and the error is correct, but what is the best way to pass the variable inside the function?
You can ask PHP to make it available in the anonymous function with the use
keyword:
$export->sheet('Example', function ($sheet) use ($data) {
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments