How to modify ToggleColumn function?

It's amazing how ToggleColumn can update to the backend, but I'm curious about how to customize that. I have a column is_primary. Once I toggled a row, the other rows is_primary must be toggled too. So if I set the is_primary to true, the other rows is_primary will be false.
ToggleColumn::make('is_primary')
ToggleColumn::make('is_primary')
Solution:
You can overwrite the behaviour via updateStateUsing()
Jump to solution
5 Replies
Solution
Dennis Koch
Dennis Koch2y ago
You can overwrite the behaviour via updateStateUsing()
vahnmarty
vahnmartyOP2y ago
Cool that worked! I hope someone can update the docs too https://filamentphp.com/docs/2.x/tables/columns/toggle hehe. but v3 is coming soon, this is gonna be exciting hehe.
Filament
Toggle column - Columns - Table Builder - Filament
The elegant TALL stack table builder for Laravel artisans.
Schilly
Schilly12mo ago
@vahnmarty what did you end up putting in the updateStateUsing? I have the exact same use case and was hoping you would share 🙂
vahnmarty
vahnmartyOP12mo ago
ToggleColumn::make('is_primary')
->label('Primary Owner')
->updateStateUsing(function (Account $record, $state): void {
$record->is_primary = $state;
$record->save();

$parents = Account::where('account_id', $record->account_id )
->where('id', '!=', $record->id)
->update([
'is_primary' => !$state
]);
})
ToggleColumn::make('is_primary')
->label('Primary Owner')
->updateStateUsing(function (Account $record, $state): void {
$record->is_primary = $state;
$record->save();

$parents = Account::where('account_id', $record->account_id )
->where('id', '!=', $record->id)
->update([
'is_primary' => !$state
]);
})
Schilly
Schilly12mo ago
Danke!

Did you find this page helpful?