Crud: Do something on record deletion?

Created on 2 Jan 2017  路  6Comments  路  Source: Laravel-Backpack/CRUD

Is there a feature to run a command when we click on the "delete" button?

Most helpful comment

@zschuessler Thanks for that. How can I extend the method on my own controller? By calling it on my controller the same way?

    public function destroy($id)
    {
        //my code
        $redirect_location = parent::destroy();
        return $redirect_location;
    }

All 6 comments

The reason I need it is because I want to also delete the record from a related table.

Checkout laravels model observers. They can hook into events like deleting so you can do more

You can extend this method if you wish to do it in the controller:

https://github.com/Laravel-Backpack/CRUD/blob/master/src/app/Http/Controllers/CrudController.php#L216

Or you have access to the Eloquent events (which is a better idea for most operations anyway):

https://laravel.com/docs/5.3/eloquent#observers

In the above, you have access to both deleting and deleted events, depending on when you want to tap into the delete action.

@zschuessler Thanks for that. How can I extend the method on my own controller? By calling it on my controller the same way?

    public function destroy($id)
    {
        //my code
        $redirect_location = parent::destroy();
        return $redirect_location;
    }

@madd86 you would create a function in your controller with the same name.

Then after your custom code you can call parent::destroy()

However I'd recommend the Observer route as it's more universal and less hacky

The issue is that the deleting observer doesn't work because according to taylor "Yes, this is basically expected. In order to fire the events we would have to pull the models first and then call the events with the model instances. Then call the delete query."

https://github.com/laravel/framework/issues/2536

Was this page helpful?
0 / 5 - 0 ratings