我的代码目前正在执行成功导出:
$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');
这里的问题是,我的收藏中有几列我想要更改,因此我添加了transform()
值的步骤:
$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');
但是,新导出现在已经完全没空了 - 电子表格会下载,但不会有任何内容。
我正在使用Laravel-Excel软件包执行导出。
为什么在执行transform()
后导出为空 - 在修改我需要的值后,如何在导出不为空之后实现我的目标?