Custom BulkAction

Hi guys, newbie here... in a table i would like to pass the selected record to a controller and then perform some operations with the data. Here is what i have When I click on the action button nothing happens
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),

BulkAction::make('print_label')
->label('print')
->color('success')
->icon('heroicon-o-printer')
->action(function($livewire) {
url(route('printlabels', ['options' => $livewire->getSelectedTableRecords()]));
})

]),
]);
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),

BulkAction::make('print_label')
->label('print')
->color('success')
->icon('heroicon-o-printer')
->action(function($livewire) {
url(route('printlabels', ['options' => $livewire->getSelectedTableRecords()]));
})

]),
]);
Thanks for the help!!
12 Replies
LeandroFerreira
LeandroFerreira7mo ago
this doesn't execute any actions. To proceed, consider implementing a request or redirection to this route.
pgferro
pgferroOP7mo ago
thanks @Leandro Ferreira, could you pls guide me to implement it ? again i'm pretty green..
Dennis Koch
Dennis Koch7mo ago
url() is a method that just generates a string. You probably need something like return redirect(...)
LeandroFerreira
LeandroFerreira7mo ago
I think you can use redirect()->action([YourController::class, 'your_method']); if you are using a controller Please check the laravel docs: https://laravel.com/docs/11.x/responses#redirecting-controller-actions
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Povilas K
Povilas K7mo ago
@pgferro here are two options to download PDF files, from our similar project - just not on BulkAction, but on a Table Action, but it should be similar for your case: Option 1: Action with Download
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Support\Facades\Blade;

// ...

Tables\Actions\Action::make('pdf')
->label('PDF')
->color('success')
->icon('heroicon-s-download')
->action(function (Model $record) {
return response()->streamDownload(function () use ($record) {
echo Pdf::loadHtml(
Blade::render('pdf', ['record' => $record])
)->stream();
}, $record->number . '.pdf');
}),
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Support\Facades\Blade;

// ...

Tables\Actions\Action::make('pdf')
->label('PDF')
->color('success')
->icon('heroicon-s-download')
->action(function (Model $record) {
return response()->streamDownload(function () use ($record) {
echo Pdf::loadHtml(
Blade::render('pdf', ['record' => $record])
)->stream();
}, $record->number . '.pdf');
}),
Option 2: Redirect to Controller that would do the work
Tables\Actions\Action::make('pdf')
->label('PDF')
->color('success')
->icon('heroicon-o-document-download')
->url(fn (Order $record) => route('pdf', $record))
->openUrlInNewTab(),

// ...

Route::get('pdf/{order}', PdfController::class)->name('pdf');

// ...

class PdfController extends Controller
{
public function __invoke(Order $order)
{
return Pdf::loadView('pdf', ['record' => $order])
->download($order->number. '.pdf');
}
}
Tables\Actions\Action::make('pdf')
->label('PDF')
->color('success')
->icon('heroicon-o-document-download')
->url(fn (Order $record) => route('pdf', $record))
->openUrlInNewTab(),

// ...

Route::get('pdf/{order}', PdfController::class)->name('pdf');

// ...

class PdfController extends Controller
{
public function __invoke(Order $order)
{
return Pdf::loadView('pdf', ['record' => $order])
->download($order->number. '.pdf');
}
}
Povilas K
Povilas K7mo ago
Full example: https://laraveldaily.com/post/filament-export-record-to-pdf-two-ways (keep in mind this was written for Filament 2, so double-check the syntax)
pgferro
pgferroOP7mo ago
thanks @PovilasKorop ! Of course I saw that article 😃 My struggle is to undertsand how to pass the collection instead of a sigle record to the controller. I tried everything I could think without success...
Povilas K
Povilas K7mo ago
Example from the docs:
BulkAction::make('delete')
->requiresConfirmation()
->action(fn (Collection $records) => ...
BulkAction::make('delete')
->requiresConfirmation()
->action(fn (Collection $records) => ...
https://filamentphp.com/docs/3.x/tables/actions#bulk-actions Doesn't work?
pgferro
pgferroOP7mo ago
@PovilasKorop i have this.
BulkAction::make('print_label')
->label('print')
->color('success')
->icon('heroicon-o-printer')
->action(function(Collection $records) {
return view('printlabel', compact('records'));
})
BulkAction::make('print_label')
->label('print')
->color('success')
->icon('heroicon-o-printer')
->action(function(Collection $records) {
return view('printlabel', compact('records'));
})
and it doesnt work, nothin happens on the click of the action
Povilas K
Povilas K7mo ago
Because return view doesn't actually return anything - it should be either downloading something or going to another URL. Check my examples above - they don't return view
krekas
krekas7mo ago
Laravel
File Downloads | Laravel
A full-stack framework for Laravel that takes the pain out of building dynamic UIs.
Povilas K
Povilas K7mo ago
Update: we published a snippet on our examples page, but to avoid being too "promotional", here's the main code:
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Blade;

public static function table(Table $table): Table
{
return $table
// ...
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\BulkAction::make('Export')
->icon('heroicon-m-arrow-down-tray')
->openUrlInNewTab()
->deselectRecordsAfterCompletion()
->action(function (Collection $records) {
return response()->streamDownload(function () use ($records) {
echo Pdf::loadHTML(
Blade::render('pdf', ['records' => $records])
)->stream();
}, 'users.pdf');
}),
]),
]);
}
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Blade;

public static function table(Table $table): Table
{
return $table
// ...
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\BulkAction::make('Export')
->icon('heroicon-m-arrow-down-tray')
->openUrlInNewTab()
->deselectRecordsAfterCompletion()
->action(function (Collection $records) {
return response()->streamDownload(function () use ($records) {
echo Pdf::loadHTML(
Blade::render('pdf', ['records' => $records])
)->stream();
}, 'users.pdf');
}),
]),
]);
}
Want results from more Discord servers?
Add your server