F
Filament12mo ago
Bagus A

Export Excel from an Action

I would like to download/export csv/xls file from an action button. The code was executed, but there's no csv/xls file downloaded after notification is triggered. How do we trigger download from inside an action?
No description
3 Replies
Vp
Vp12mo ago
return Excel::downlo....
Jon Mason
Jon Mason12mo ago
Here's how I'm currently doing it:
public function exportAction()
{
return Action::make('export')
->size('lg')
->color('gray')
->iconButton()
->icon('heroicon-o-arrow-down-tray')
->tooltip('Download Transactions')
->action(fn (array $arguments) => $this->exportToExcel($arguments))

}
public function exportAction()
{
return Action::make('export')
->size('lg')
->color('gray')
->iconButton()
->icon('heroicon-o-arrow-down-tray')
->tooltip('Download Transactions')
->action(fn (array $arguments) => $this->exportToExcel($arguments))

}
public function exportToExcel()
{
$name = $this->statement->display_name;
$fileName = Str::slug($name) . '-transactions-' . Timezone::now()->format('Y-m-d') . '.csv';

return Excel::download(new ReconciliationTransactionsExport($this->statement), $fileName, \Maatwebsite\Excel\Excel::CSV);
}
public function exportToExcel()
{
$name = $this->statement->display_name;
$fileName = Str::slug($name) . '-transactions-' . Timezone::now()->format('Y-m-d') . '.csv';

return Excel::download(new ReconciliationTransactionsExport($this->statement), $fileName, \Maatwebsite\Excel\Excel::CSV);
}
In my blade:
{{ ($this->exportAction)(['id' => $statement->id]) }}
{{ ($this->exportAction)(['id' => $statement->id]) }}
Works flawlessly. Hope this helps. If you haven't checked out Maatwebsite package for excel, it's super easy.
Bagus A
Bagus AOP12mo ago
Thanks @Jon Mason . My code actually working, but I didn't return the Excel:download... part. It need to be returned to download the file.
Want results from more Discord servers?
Add your server