Introduction
Filament’s tables can use Actions. They are buttons that can be added to the end of any table row, or even in the header or toolbar of a table. For instance, you may want an action to “create” a new record in the header, and then “edit” and “delete” actions on each row. Bulk actions can be used to execute code when records in the table are selected. Additionally, actions can be added to any table column, such that each cell in that column is a trigger for your action. It’s highly advised that you read the documentation about customizing action trigger buttons and action modals to that you are aware of the full capabilities of actions.Record actions
Action buttons can be rendered at the end of each table row. You can put them in the$table->recordActions() method:
make() method, passing its unique name.
You can then pass a function to action() which executes the task, or a function to url() which creates a link:
$record that was clicked.
Positioning record actions before columns
By default, the record actions in your table are rendered in the final cell of each row. You may move them before the columns by using theposition argument:
Positioning record actions before the checkbox column
By default, the record actions in your table are rendered in the final cell of each row. You may move them before the checkbox column by using theposition argument:
Global record action settings
To customize the default configuration used for ungrouped record actions, you can usemodifyUngroupedRecordActionsUsing() from a Table::configureUsing() function in the boot() method of a service provider:
Accessing the selected table rows
You may want an action to be able to access all the selected rows in the table. Usually, this is done with a bulk action in the header of the table. However, you may want to do this with a row action, where the selected rows provide context for the action. For example, you may want to have a row action that copies the row data to all the selected records. To force the table to be selectable, even if there aren’t bulk actions defined, you need to use theselectable() method. To allow the action to access the selected records, you need to use the accessSelectedRecords() method. Then, you can use the $selectedRecords parameter in your action to access the selected records:
Bulk actions
Tables also support “bulk actions”. These can be used when the user selects rows in the table. Traditionally, when rows are selected, a “bulk actions” button appears. When the user clicks this button, they are presented with a dropdown menu of actions to choose from. You can put them in the$table->toolbarActions() or $table->headerActions() methods:
make() method, passing its unique name. You should then pass a callback to action() which executes the task:
$records that are selected. It is an Eloquent collection of models.
Authorizing bulk actions
When using a bulk action, you may check a policy method for each record that is selected. This is useful for checking if the user has permission to perform the action on each record. You can use theauthorizeIndividualRecords() method, passing the name of a policy method, which will be called for each record. If the policy denies authorization, the record will not be present in the bulk action’s $records parameter:
Bulk action notifications
After a bulk action is completed, you may want to send a notification to the user with a summary of the action’s success. This is especially useful if you’re using authorization for individual records, as the user may not know how many records were actually affected. To send a notification after the bulk action is completed, you should set thesuccessNotificationTitle() and failureNotificationTitle():
- The
successNotificationTitle()is used as the title of the notification when all records have been successfully processed. - The
failureNotificationTitle()is used as the title of the notification when some or all of the records failed to be processed. By passing a function to this methods, you can inject the$successCountand$failureCountparameters, to provide this information to the user.
DenyResponse and replaces Response::deny(), allowing the developer to pass a function as the message which can receive information about how many records were denied by that authorization check:
make() method is a unique key to identify that failure type. If multiple failures of that key are detected, they are grouped together and only one message is generated. If there are multiple points of failure in the policy method, each response object can have its own key, and the messages will be concatenated together in the notification.
Reporting failures in bulk action processing
Alongside individual record authorization messages, you can also report failures in the bulk action processing itself. This is useful if you want to provide a message for each record that failed to be processed for a particular reason, even after authorization passes. This is done by injecting theAction instance into the action() function, and calling the reportBulkProcessingFailure() method on it, passing a key and message function similar to DenyResponse:
delete() method on an Eloquent model returns false if the deletion fails, so you can use that to determine if the record was deleted successfully. The reportBulkProcessingFailure() method will then add a failure message to the notification, which will be displayed when the action is completed.
The reportBulkProcessingFailure() method can be called at multiple points during the action execution for different reasons, but you should only call it once per record. You should not proceed with the action for that particular record once you have called the method for it.
Grouping bulk actions
You may use aBulkActionGroup object to group multiple bulk actions together in a dropdown. Any bulk actions that remain outside the BulkActionGroup will be rendered next to the dropdown’s trigger button:
groupedBulkActions() method:
Deselecting records once a bulk action has finished
You may deselect the records after a bulk action has been executed using thedeselectRecordsAfterCompletion() method:
Disabling bulk actions for some rows
You may conditionally disable bulk actions for a specific record:Limiting the number of selectable records
You may restrict how many records the user can select in total:Preventing bulk-selection of all pages
TheselectCurrentPageOnly() method can be used to prevent the user from easily bulk-selecting all records in the table at once, and instead only allows them to select one page at a time:
Restricting bulk selection to groups only
TheselectGroupsOnly() method can be used to restrict bulk selection to only records within the same group, preventing bulk selection across multiple groups at once:
Improving the performance of bulk actions
By default, a bulk action will load all Eloquent records into memory before passing them to theaction() function.
If you are processing a large number of records, you may want to use the chunkSelectedRecords() method to fetch a smaller number of records at a time. This will reduce the memory usage of your application:
$records collection as normal, but the collection will be a LazyCollection instead of a normal collection.
You can also prevent Filament from fetching the Eloquent models in the first place, and instead just pass the IDs of the selected records to the action() function. This is useful if you are processing a large number of records, and you don’t need to load them into memory:
Header actions
Both actions and bulk actions can be rendered in the header of the table. You can put them in the$table->headerActions() method:
Toolbar actions
Both actions and bulk actions can be rendered in the toolbar of the table. You can put them in the$table->toolbarActions() method:
Column actions
Actions can be added to columns, such that when a cell in that column is clicked, it acts as the trigger for an action. You can learn more about column actions in the documentation.Grouping actions
You may use anActionGroup object to group multiple table actions together in a dropdown: