Best way to handle DeleteAction integrity constraint violations

Hi all I've got a model with a restrictOnDelete relationship, so when I click the DeleteAction I get an integrity contraint violation. I want to show a notification explaining why the record can't be deleted. What's the best way to handle this? Using the before() lifecycle hook? Or is there a better way? Thanks
Solution:
Yeah just use ->using() on that delete action
Jump to solution
5 Replies
ZedoX
ZedoX14mo ago
Hi i have done it like this in one of the projects.
//in boot method of a service provider
DeleteAction::configureUsing(function ($action) {
$action->using(static function (Model $record) {
try {
return $record->delete();
} catch (\Exception $e) {
Notification::make()
->title('Unable to delete')
->danger()
->send();
return false;
}
});
});
//in boot method of a service provider
DeleteAction::configureUsing(function ($action) {
$action->using(static function (Model $record) {
try {
return $record->delete();
} catch (\Exception $e) {
Notification::make()
->title('Unable to delete')
->danger()
->send();
return false;
}
});
});
binaryfire
binaryfireOP14mo ago
@ZedoX Do you know of a way to do it for a specific delete action rather than globally? I need to return a pretty detailed notification message for this one.
Solution
ZedoX
ZedoX14mo ago
Yeah just use ->using() on that delete action
ZedoX
ZedoX14mo ago
And provide your custom deletion logic
binaryfire
binaryfireOP14mo ago
@ZedoX Nice thanks

Did you find this page helpful?