Tricks

Toggleable BooleanColumn

Jul 25, 2022
Hörmann Bernhard
Table builder

Wouldn't it be great, to change the value of a boolean column (for example a status) without leaving the table view? There is already a build in solution for it in filament:

Tables\Columns\BooleanColumn::make('status')
->action(function($record, $column) {
$name = $column->getName();
$record->update([
$name => !$record->$name
]);
});

Result:

You can make this even reusable, by adding it as a macro to the BooleanColumn. In your AppServiceProvider, add this to your boot method:

Filament::serving(function(){
\Filament\Tables\Columns\BooleanColumn::macro('toggle', function() {
$this->action(function($record, $column) {
$name = $column->getName();
$record->update([
$name => !$record->$name
]);
});
return $this;
});
});

and now you can make BooleanColumn's with a single method clickable:

Tables\Columns\BooleanColumn::make('status')
->toggle();
avatar

Thanks for the trick ! Really useful !

avatar

How to add confirmation dialog?

avatar

This does not work anymore!

avatar

still works, you just have to create the macro on IconColumn since BooleanColumn is now deprecated. And then use it like so

IconColumn::make('active')
->boolean()
->toggle(),
avatar

Can this be applied also to pivot relations as it does nothing when this is a pivot relation?