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();
Thanks for the trick ! Really useful !
How to add confirmation dialog?
This does not work anymore!
still works, you just have to create the macro on
IconColumn
sinceBooleanColumn
is now deprecated. And then use it like soCan this be applied also to pivot relations as it does nothing when this is a pivot relation?