->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?
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:
this will disable the toggle button it altogether if status is set active.
->disabled(fn ($record) => $record->status)this will disable the toggle button it altogether if status is set active.