Filament: ToggleColumn: Only One Active Record (Set Others to False)

Free Snippet

If you have a ToggleColumn in the table and want to ensure there's only ONE row in the database with true value, you can auto-set others to false.


How it works

All you need to do is use the function beforeStateUpdated() on the ToggleColumn and run one update Eloquent query inside.

For example, if you have a DB column as boolean, here's the code:


public static function table(Table $table): Table
return $table
->beforeStateUpdated(function (User $record) {
User::where('id', '!=', $record->id)->update(['active' => false]);

The table will refresh automatically.

A few of our Premium Examples: