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
Vp7mo ago
return Excel::downlo....
Jon Mason
Jon Mason7mo 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 A6mo 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.