Laravel 8 上有一个项目,为管理面板安装了 Orchid,在一个页面上我将用户上传到 Excel 文件,此上传需要考虑已安装的过滤器。这就是我现在的文件的样子:
public function query(): iterable {
return [
'participant' => Profile::when(Auth::user()->organizer_id, function (Builder $query) {
return $query->where('organizer_id', Auth::user()->organizer_id);
})->filters()->defaultSort('id', 'desc')->paginate(),
];
}
public function commandBar(): iterable {
return [
Button::make('Выгрузить в Excel')
->method('export')
->icon('cloud-download'),
];
}
public function export(Request $request) {
$profiles = Profile::when(Auth::user()->organizer_id, function (Builder $query) {
return $query->where('organizer_id', Auth::user()->organizer_id);
})->filters()->select('external_id', 'last_name', 'first_name', 'patronymic', 'phone', 'email', 'gender',
'date_of_birth', 'status')->defaultSort('id', 'desc')->get();
$fileArray = array_merge([['Внешний id', 'Фамилия', 'Имя', 'Отчество', 'Номер телефона',
'Email', 'Пол', 'Дата рождения', 'Статус']], $profiles->toArray());
$spreadsheet = new Spreadsheet();
$activeWorksheet = $spreadsheet->getActiveSheet();
$activeWorksheet->getStyle('A1:I' . count($fileArray))->getNumberFormat()
->setFormatCode(NumberFormat::FORMAT_TEXT);
$widthColumn = '20';
$activeWorksheet->getColumnDimension('A')->setWidth($widthColumn);
$activeWorksheet->getColumnDimension('B')->setWidth($widthColumn);
$activeWorksheet->getColumnDimension('C')->setWidth($widthColumn);
$activeWorksheet->getColumnDimension('D')->setWidth($widthColumn);
$activeWorksheet->getColumnDimension('E')->setWidth($widthColumn);
$activeWorksheet->getColumnDimension('F')->setWidth($widthColumn);
$activeWorksheet->getColumnDimension('G')->setWidth($widthColumn);
$activeWorksheet->getColumnDimension('H')->setWidth($widthColumn);
$activeWorksheet->getColumnDimension('I')->setWidth($widthColumn);
$activeWorksheet->fromArray($fileArray);
$writer = new Xlsx($spreadsheet);
return response()->streamDownload(function () use ($writer) {
$writer->save('php://output');
}, 'profiles.xlsx', [
'Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'Content-Disposition: attachment;filename="profiles.xlsx"',
'Cache-Control: max-age=0',
'Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT',
'Cache-Control: cache, must-revalidate',
'Pragma: public'
]);
}
这有效并卸载了用户,但我无法想象如何传递数据以供过滤器导出。$request 中没有过滤数据。我需要你的帮助。
最后,我设法这样做了,尽管它看起来像拐杖: