filament-excel icon indicating copy to clipboard operation
filament-excel copied to clipboard

Exporting via route?

Open Sanabria opened this issue 2 years ago • 1 comments

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

Sanabria avatar Oct 17 '22 15:10 Sanabria

Not sure how this would work, as a lot of the logic is coupled to the Livewire components.

pxlrbt avatar Oct 17 '22 16:10 pxlrbt

@Sanabria in 4 steps i think:

  1. Create a model with migration lets say Reports with name,path and generated_by columns(you can name them as you want).
  2. create a listener for ExportFinishedEvent, this event has two parameters $filename and userId
public function handle(ExportFinishedEvent $event)
   {
       Report::create([
           'name' => substr($event->filename, 37),
           'path' => $event->filename,
           'generated_by' => User::find($event->userId)->name
       ]);
   }
  1. 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');
  1. 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()
Screenshot 2022-11-16 at 9 49 45 PM

@pxlrbt you can close it now i think.

bezhanSalleh avatar Nov 16 '22 17:11 bezhanSalleh

Thanks @bezhanSalleh

pxlrbt avatar Nov 16 '22 20:11 pxlrbt