How to check if there are related records before deleting to show user friendly error
This can be achieved by using before
lifecycle hook on DeleteAction
. We can then call action->halt()
or action->cancel()
from inside of this lifecycle hook.
Example: Here we want to cancel the delete action and close the modal too if the category has subcategories.
Tables\Actions\DeleteAction::make() ->before(function (Tables\Actions\DeleteAction $action, Category $record) { if ($record->childrenCategories()->exists()) { Notification::make() ->danger() ->title('Failed to delete!') ->body('Category has subcategories.') ->persistent() ->send(); // This will halt and cancel the delete action modal. $action->cancel(); } }),
No comments yet…