DEV3 Technologies home

DEV3 Developer Forum

How do I add a custom row action and bulk action to a Filament 4 table?

Laravel helpSolved0 views
dev3 ·

In my Filament 4 resource I want a Mark as shipped button per order row and a bulk action to mark many orders shipped, with a confirmation and a notification. Imports changed from Filament 3 and I keep getting class not found errors.

#laravel#filament

0

1 reply

dev3 · Accepted answer

In Filament 4 the namespaces for actions were unified: all actions (table row, bulk, header and form actions) now live in Filament\Actions, not Filament\Tables\Actions. That is the usual cause of class-not-found after following Filament 3 tutorials.

use Filament\Actions\Action;
use Filament\Actions\BulkAction;
use Filament\Actions\BulkActionGroup;
use Filament\Notifications\Notification;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Collection;

public static function table(Table $table): Table
{
    return $table
        ->columns([ /* ... */ ])
        ->recordActions([
            Action::make('ship')
                ->label('Mark as shipped')
                ->icon('heroicon-o-truck')
                ->requiresConfirmation()
                ->visible(fn (Order $record) => $record->status === 'paid')
                ->action(function (Order $record) {
                    $record->update(['status' => 'shipped', 'shipped_at' => now()]);
                    Notification::make()->title('Order shipped')->success()->send();
                }),
        ])
        ->toolbarActions([
            BulkActionGroup::make([
                BulkAction::make('shipMany')
                    ->label('Mark selected as shipped')
                    ->requiresConfirmation()
                    ->deselectRecordsAfterCompletion()
                    ->action(fn (Collection $records) => $records->each->update(['status' => 'shipped'])),
            ]),
        ]);
}

Method names also changed from Filament 3: ->actions() became ->recordActions() and ->bulkActions() became ->toolbarActions().

Forms inside actions: add ->schema([...]) (Filament 4) with form fields, and the data arrives as array $data in ->action().

Pitfalls

  • For many rows use a queued job inside the bulk action instead of updating inline, and send a notification when it finishes.
  • Authorize actions with ->authorize('update') or by ->visible(); hiding is not security, so also check in the action or policy.
  • Run php artisan filament:upgrade after Composer updates, and see the official v3 to v4 upgrade guide (an automated upgrade script exists).
0

Your reply

Every post is reviewed by DEV3 before it appears. Usually reviewed within 24 hours.

Markdown supported. Use ``` for code blocks. Images are not allowed.