I created a toggle column in table and I want on specific condition satisfies then only state must be updated. so i used beforeStateUpdated(), and returned false in it but it didn't worked. So what should i return to stop updating it on being updated.
@jatanbhimani BTW, wrap your code by 3 ` to highlight properly. Check details here
I don't think you can simply return false. Instead, I will go for the defensive way first. How about this?
ToggleColumn::make('status')
->beforeStateUpdated(function ($record, $state) {
// Replace the following condition with your actual logic
if ("condition does not match") {
// Notify the user that the condition is not met
Notification::make()
->title('Title for condition not met')
->body('Some message for the user')
->danger()
->send();
// Throw a validation exception to stop the state update
throw ValidationException::withMessages([
'status' => 'The status cannot be updated because the specific condition is not met.',
]);
}
})
->afterStateUpdated(function ($record, $state) {
// Notify about attempted status change
Notification::make()
->title('Status updated!')
->color('success')
->send();
});