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();

No comments yet…