I have this code that is currently performing a successful export:
$ncrforms = NCRForm::where('created_at', '>=', (new Carbon('first day of last month'))->toDateString())
->where('created_at', '<=', (new Carbon('last day of last month'))->toDateString())
->get();
Excel::create('ncr_forms', function($excel) use($ncrforms) {
$excel->sheet('Previous Months Forms', function($sheet) use($ncrforms) {
$sheet->fromArray($ncrforms);
});
})->export('xls');
The issue here, though, is that I have a couple of columns from my collection that I want to change, and so I have added a step to transform()
the values:
$ncrforms = NCRForm::where('created_at', '>=', (new Carbon('first day of last month'))->toDateString())
->where('created_at', '<=', (new Carbon('last day of last month'))->toDateString())
->get();
$ncrforms->transform(function($form) {
$form->responsibility = is_null($form->responsibility) ? $form->responsibility : $form->personResponsible->name;
$form->user_id = $form->user->name;
return $form;
});
Excel::create('ncr_forms', function($excel) use($ncrforms) {
$excel->sheet('Previous Months Forms', function($sheet) use($ncrforms) {
$sheet->fromArray($ncrforms);
});
})->export('xls');
However, the new export is now completely empty - the spreadsheet downloads but nothing is in it.
I am using the Laravel-Excel package to perform the export.
Why is the export empty after I perform the transform()
- how can I achieve what I am after where the export isn't empty after modifying the values I need to?