Bulk Actions
Introduction
Bulk Actions can be used to perform actions on multiple rows in a table. Bulk Actions can be configured to require a confirmation dialog before performing the action. Just like Row Actions, Bulk Actions are defined in the actions()
method of the Table component.
Defining Bulk Actions
Bulk Actions are defined exactly like Row Actions, but you can use the asBulkAction
and onlyAsBulkAction
methods to specify whether the action should be available as a bulk action only or as a row action as well.
use App\Models\User;
use InertiaUI\Table\Action;
class Users extends Table
{
public function actions(): array
{
return [
Action::make('Activate', handle: fn (User $user) => $user->activate())->asBulkAction(),
];
}
}
Styling, authorization, confirmation dialogs, and redirecting after the action are all handled the same way as Row Actions.
Before and After callbacks
You can use the before
and after
arguments to define callbacks that should be executed before and after the action is performed. Both callbacks receive an array of Model IDs as the first argument.
Action::make(
label: 'Activate',
before: fn (array $ids) => Log::info('Activating users...', ['ids' => $ids]),
handle: fn (User $user) => $user->activate(),
after: fn () => session()->flash('success', 'Users activated successfully')
)->asBulkAction();
Chunk Size
The default chunk size for bulk actions is 1000
. You can change this by passing the chunkSize
argument to the make()
method:
Action::make(
label: 'Activate',
handle: fn (User $user) => $user->activate(),
chunkSize: 500
)->asBulkAction();
Chunk Method
By default, the database will be queried using the chunkById
method. If you want to use the chunk
method instead, you can pass the eachById
argument to the make()
method with a value of false
:
Action::make(
label: 'Activate',
handle: fn (User $user) => $user->activate(),
eachById: false
)->asBulkAction();
You can read more about the differences between chunkById
and chunk
in the Laravel documentation. It is generally recommended to use chunkById
when you're updating or deleting records.