Laravel 5 Delete Records in your table, Beginner’s Guide

Laravel 5 Delete Records in your table, Beginner’s Guide

SHARE

Deleting records in Laravel is very easy.

Since you can’t delete records via GET method (of course you can). No, you should not since if you use GET method in deleting a record, user might delete your records in the URL directly, and that’s a security problem.

Note: If you don’t want to entirely delete your record you can use SoftDeletes() in Laravel. See How to Soft Delete on Laravel 5

So, we have to pass on a POST method in the view to the controller. Doing this will allow the deletion to occur behind the scene.

This is the last part of the CRUD process, and you should know by now how to Create, Read and Update records. So I’m not going into details about the controller or model records.

To do this, in your index.blade.php (to wherever you want to put your delete button) we will add a new form. The form will contain our delete button, and will submit a POST data to our controller. I included an edit button also, just in case you needed some buttons beside you delete. Just make sure you put the added buttons inside the form. (I always use my own work, I used the Office as my sample model)

It seems a lot, but basically it just table and I added some Bootstrap class to it, some glyphicon to make it look nicer (you can remove it, if you want a simple delete button). I also added also nice bootstrap tooltip to make it even more cooler.

So, We created a form, inside our table. The method should be DELETE, which is specified in your route list. And the route should lead you to the destroy() function of your Controller. The form WILL SUBMIT THE ID of the record you want to delete.

We add a delete button type submit, and added a prompt to it by adding a JavaScript in onclick() attribute.

The above code will looked like this.

delete record

In your controller destroy() function add the following

The destroy() function accepts the ID the delete form passed. We find that ID in the model, and just delete it.

We added a nice flash message that will appear in the view to notify user about the deletion, then it will redirect back to the view. See Flash Messages in Laravel 5 for more info about Flash Messages.

SHARE
Programmer, Database Designer and Administrator for Web Application. Who loves to read and write interesting topics.

26 COMMENTS

  1. Reading your website is big pleasure for me, it deserves to
    go viral, you need some initial traffic only. If you want to know
    how to get it search for: blackhatworren’s strategies

  2. I simply want to mention I am just all new to blogging and site-building and really loved this web blog. Very likely I’m planning to bookmark your blog post . You certainly have outstanding well written articles. Thanks for revealing your webpage.

  3. Unquestionably consider that that you said.
    Your favorite justification seemed to be at the internet the easiest thing to consider of.

    I say to you, I definitely get annoyed whilst other folks think
    about issues that they just don’t understand about. You managed to hit the nail upon the top and defined out the whole thing without
    having side-effects , folks can take a signal. Will likely be
    again to get more. Thank you http://www.Maichenlindengshi.com/comment/html/index.php?page=1&id=103460

  4. Hi there I am so grateful I found your blog page, I really found you by error,
    while I was looking on Bing for something else, Anyways I am here now and would just
    like to say thanks a lot for a remarkable post and a all round
    exciting blog (I also love the theme/design), I don’t have time to read through it all at the minute but I have saved it and also included your RSS feeds,
    so when I have time I will be back to read much more, Please do keep
    up the awesome b. http://refreshem.co.za/index.php/component/k2/itemlist/user/1391569

LEAVE A REPLY