Sample Filament Actions
FilamentHere is some sample source code to Filament actions. This source code is from InvoiceWizard.
Attributes about actions:
- Actions are typically set in resources, the main resource file. There might be other places to set them as well.
- Actions can be "normal" actions, and bulk actions.
- Actions typically use closures
- Some closures use the shorthand notation
- Other closures that perform more complex tasks need the traditional notation
Here is a bulk action example:
->bulkActions([
BulkAction::make('Fetch Quote')
->action(function ($records) {
foreach ($records as $record) {
FetchQuote::dispatch($record, true); // Ignore cache
}
})->requiresConfirmation(),
lang-phpHere is another example showing both a normal action and a bulk action:
->actions([
Action::make('View')
->url(fn (Invoice $record): string => url('admin/invoices/pdf', $record->id))
->label('PDF'),
Tables\Actions\EditAction::make(),
])
->bulkActions([
BulkAction::make('Email Invoice')
->action(function ($records) {
foreach ($records as $record) {
InvoiceService::createPdf($record);
Mail::send(new InvoiceCreated($record));
}
})->requiresConfirmation(),
BulkAction::make('applyPayment')
->action(function (Collection $records, array $data): void {
ApplyPayment::handle(
$records,
$data
);
})
->form([
Forms\Components\DatePicker::make('date')
->label('Payment Date')
->displayFormat('Y-m-d')
->default(now())
->required(),
Forms\Components\TextInput::make('amount')
->label('Amount')
->required(),
Forms\Components\TextInput::make('reference')
->label('Bank Reference')
->required(),
]),
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
Tables\Actions\ForceDeleteBulkAction::make(),
Tables\Actions\RestoreBulkAction::make(),
]),
])
lang-php