F
Filament6mo ago
Mikail

->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:
Solved it with: ->disabled(fn ($record) => $record->status) this will disable the toggle button it altogether if status is set active....
Jump to solution
3 Replies
Mikail
MikailOP6mo ago
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
Mikail
Mikail6mo ago
Solved it with: ->disabled(fn ($record) => $record->status) this will disable the toggle button it altogether if status is set active.
Want results from more Discord servers?
Add your server