Table Bulk Actions

The Table component supports performing Bulk Actions. First, you must register a supporting route using the spladeTable() method on the Route facade. As of version 0.6, the automatic installer does this for you. If you need to register the route manually, make sure it uses the web and splade Middleware, for example, in web.php:

Route::middleware('splade')->group(function () {
Route::spladeTable();
});

Configure a Bulk Action

You may configure a Bulk Action on the SpladeTable instance:

public function configure(SpladeTable $table)
{
$table->bulkAction('Touch timestamp', function (Project $project) {
$project->touch();
});
}

The bulkAction method has additional before and after arguments. You may use this to show a Toast when the action has finished, or for example, to perform some logging:

$table->bulkAction(
label: 'Touch timestamp',
each: fn (Project $project) => $project->touch(),
before: fn () => info('Touching the selected projects'),
after: fn () => Toast::info('Timestamps updated!')
);

The before and after callbacks receive the selected rows as an argument. You may use this to perform additional actions, for example, to send an email to the selected users:

$table->bulkAction(
label: 'Notify users',
before: function (array $selectedIds) {
$users = User::whereIn('id', $selectedIds)->get();
 
Mail::to($users)->send(new ImportantNotification);
}
);

Note that when all rows are selected, the callbacks receive an array with a single * item:

function (array $selectedIds) {
$users = User::query()
->unless($selectedIds === ['*'], fn ($query) => $query->whereIn('id', $selectedIds))
->get();
 
Mail::to($users)->send(new ImportantNotification);
}

Confirmation

You may use the confirm argument to show a confirmation dialog before Splade performs the action:

$table->bulkAction(
label: 'Touch timestamp',
each: fn (Project $project) => $project->touch(),
confirm: true
);

In addition, you may customize the confirmation dialog:

$table->bulkAction(
label: 'Touch timestamp',
each: fn (Project $project) => $project->touch(),
confirm: 'Touch projects',
confirmText: 'Are you sure you want to touch the projects?',
confirmButton: 'Yes, touch all selected rows!',
cancelButton: 'No, do not touch!',
);

Password Confirmation

It's even possible to require the user to confirm their password within the confirmation dialog. First, you must register a supporting route using the spladePasswordConfirmation() method on the Route facade. As of version 1.2.2, the automatic installer does this for you. If you need to register the route manually, make sure it uses the web Middleware, for example, in web.php:

Route::spladePasswordConfirmation();

Now you may set the requirePassword argument to true:

$table->bulkAction(
label: 'Delete projects',
each: fn (Project $project) => $project->delete(),
confirm: true,
requirePassword: true
);

Authorization

Just like Form Requests, you may use the authorize method to determine if the user has the authority to perform a Bulk Action:

class Projects extends AbstractTable
{
public function authorize(Request $request)
{
return $request->user()->is_admin;
}
}