filament-excel
filament-excel copied to clipboard
Exporting via route?
Would it be possible to export the file by accessing a route? Something like what Laravel Excel does: https://docs.laravel-excel.com/3.1/exports/#_5-minute-quick-start
I would like to initiate the export with a button placed in the dashboard, rather than from a headeraction/bulkaction.
Thanks in advance
Not sure how this would work, as a lot of the logic is coupled to the Livewire components.
@Sanabria in 4 steps i think:
- Create a model with migration lets say
Reports
withname
,path
andgenerated_by
columns(you can name them as you want). - create a listener for
ExportFinishedEvent
, this event has two parameters$filename
anduserId
public function handle(ExportFinishedEvent $event)
{
Report::create([
'name' => substr($event->filename, 37),
'path' => $event->filename,
'generated_by' => User::find($event->userId)->name
]);
}
- Add this route:
Route::get('download-report/{path}', function (string $path) {
return response()->download(
Storage::disk('filament-excel')
->path($path),
substr($path, 37)
);
})
->where('path', '.*')
->name('download-report');
- Create a resource with only the index page. you don't need the rest.
you can download the report either via column:
Tables\Columns\TextColumn::make('name')
->url(fn ($record): string => route('download-report', ['path' => $record->path])),
or table action:
Tables\Actions\Action::make('download')
->icon('heroicon-o-download')
->color('warning')
->button()
->url(fn ($record): string => route('download-report', ['path' => $record->path]))
->openUrlInNewTab()

@pxlrbt you can close it now i think.
Thanks @bezhanSalleh