->updateStateUsing on ToggleColumn
There must be an active record no matter what in the resource which works fine in the database with a modification using ->updateStateUsing as shown in code below. The only issue is that toggle button won't go back to 'on' in the view until browser refresh.
Anything I could do?
ToggleColumn::make('status')
->label('Active')
->updateStateUsing(function ($record, $state) {
if (!$state) {
$schoolId = Filament::getTenant()->id;
$hasOtherActive = $record::where('id', '!=', $record->id)
->where('school_id', $schoolId)
->where('status', true)
->exists();
if (!$hasOtherActive) {
Notification::make()
->danger()
->title('Update Failed')
->body('There must be at least one active session.')
->send();
return $state = true;
}
} else {
return $record->update(['status' => $state]);
}
})
Solution:Jump to solution
Solved it with:
->disabled(fn ($record) => $record->status)
this will disable the toggle button it altogether if status is set active....3 Replies
you want like this? https://filamentexamples.com/project/togglecolumn-one-active-set-others-false
Thanks, this similar, I did achieve only one active record but it went further. If a record is turned off it should be on. There must be at least one active record. The code above make sure it doesn't persist when you turn off but the toggle itself turns off and wont be on until browser refresh. Hope yu get my point...
Solution
Solved it with:
->disabled(fn ($record) => $record->status)
this will disable the toggle button it altogether if status is set active.