F
Filamentβ€’17mo ago
vahnmarty

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 Kochβ€’17mo ago
You can overwrite the behaviour via updateStateUsing()
vahnmarty
vahnmartyOPβ€’17mo 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
Schillyβ€’10mo 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
vahnmartyOPβ€’10mo 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
Schillyβ€’10mo ago
Danke!
Want results from more Discord servers?
Add your server