Search code examples
phplaravellaravel-admin

How to add your exceptions when deleting an entity having a relations on Laravel Admin?


When deleting an entity in Laravel-Admin having a relationship, a not-so-informative error dialog box crashes from the user's point of view.

How to handle this exception in the framework of Laravel-Admin in order to give the user an informative error message?

Thank you for your suggestions.

enter image description here


Solution

  • You should add ->onDelete('cascade') on your foreign key in migration.

    Example: $table->foreign('point_sale_online_id')->references('id')->on('point_sale_online')->onDelete('cascade');

    But I guess that you want to achieve that user has to validate deleting something. The way I do this: delete button fires modal with confirm button, which is a submit to the form with SomethingController@destroy action. Nevertheless to achieve that you need to add this onDelete method. Then you can simply do

    if(App\Something::find($id)->delete(){
    //all good code
    } else {
    // something went wrong code
    }