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();
Make rows unselectable
You can define whether rows should be selectable or not by using the isSelectable
method on your Table class:
use Illuminate\Database\Eloquent\Model;
class Users extends Table
{
public function isSelectable(Model $model): bool
{
return ! $model->trashed();
}
}
Please note that this only disables the checkbox for the row. The Row Action will still be executable on the row as long as you don't disable it. Please refer to the Row Actions documentation for more information on how to disable Row Actions.
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.
Authorization
You can use the authorize
argument to define a boolean or callback that determines if the action should be available for the current user. The callback should return a boolean value and accepts the current Request instance as the first argument. By not authorizing the Bulk Action, it will be disabled in the Bulk Actions dropdown.
Action::make(
label: 'Delete',
handle: fn (User $user) => $user->delete(),
authorize: fn (Request $request) => $request->user()->can('delete', User::class)
)->asBulkAction();
In earlier versions of the package, the disabled
and hidden
methods didn't exist, and authorize
was the only way to disable or hide an action. However, the Model instance is not passed to the authorize
callback, so you can only authorize it globally and not per row. Please refer to the Row Actions documentation for more information on how to disable Row Actions.
Data Attributes and Metadata
Check out the Row Links documentation for more information on how to add Data Attributes and Metadata to Bulk Actions.