{"id":19057,"date":"2020-10-23T10:38:45","date_gmt":"2020-10-23T10:38:45","guid":{"rendered":"http:\/\/www.devopsschool.com\/blog\/?p=19057"},"modified":"2022-04-28T09:15:10","modified_gmt":"2022-04-28T09:15:10","slug":"how-to-delete-multiple-records-using-checkbox-in-laravel","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/how-to-delete-multiple-records-using-checkbox-in-laravel\/","title":{"rendered":"How to Delete Multiple Records using Checkbox in Laravel?"},"content":{"rendered":"\n<p>It&#8217;s almost need to give feature for remove multiple records using checkbox, if you are developing e-commerce application or any big web application then you must give feature to delete multiple records.<\/p>\n\n\n\n<p>So in this post, i will let you know how to delete multiple records with checkbox in laravel 5, laravel 6, laravel 7 and laravel 8 application. here i also give multiple delete and you can also delete single records.&nbsp;<\/p>\n\n\n\n<p>In this example, i simply created &#8220;products&#8221; table with id, name, details, created_at and updated_at columns. I also added mysql query for add dummy records. Here i use jquery for select all checkboxs and delete all records. So finally you have to follow some step and get the layout like as bellow.<\/p>\n\n\n\n<p><strong>Step 1: Create Project for this command<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">$ composer create-project --prefer-dist laravel\/laravel Multiple_delete <span class=\"hljs-string\">\"5.8.*\"<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/10\/first-Step-1.png\" alt=\"\" class=\"wp-image-19062\"\/><\/figure>\n\n\n\n<p><strong>Step 2 : <strong>Go to laravel Project and .env file<\/strong><\/strong> <strong><strong>set msql name <\/strong><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/10\/env-1024x557.png\" alt=\"\" class=\"wp-image-19066\"\/><\/figure>\n\n\n\n<p><strong>Step 3 : <strong><strong>&nbsp;Create database<\/strong><\/strong><\/strong> <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/10\/database-1024x491.png\" alt=\"\" class=\"wp-image-19069\"\/><\/figure>\n\n\n\n<p><strong>Step 4 : <strong>Create<\/strong><\/strong> <strong>ProductController for this Command<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">$ php artisan make:controller Product\n<\/code><\/span><\/pre>\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/10\/product.png\" alt=\"\" class=\"wp-image-19064\"\/><\/figure>\n\n\n\n<p><strong>Step 5 : <strong>Create<\/strong><\/strong> <strong>Product migration for this command<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">$ php artisan make:migration create_product_table\n<\/code><\/span><\/pre>\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/10\/migration.png\" alt=\"\" class=\"wp-image-19073\"\/><\/figure>\n\n\n\n<p><strong>Step 5:<\/strong> <strong>Product migration add for this Product name and Product details<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/10\/table-add-1024x553.png\" alt=\"\" class=\"wp-image-19074\"\/><\/figure>\n\n\n\n<p><strong>Step 6: Create products Table for database this command<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">$ php artisan migrate\n<\/code><\/span><\/pre>\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/10\/migrate.png\" alt=\"\" class=\"wp-image-19075\"\/><\/figure>\n\n\n\n<p><strong>Step 7: <strong><strong>Add ProductController<\/strong><\/strong><\/strong><\/p>\n\n\n\n<p>Here, we will create new ProductController file to handle request of created three new route. In this Controller we define three method, index(), destroy() and deleteAll(). method will handle route request. So let&#8217;s create new controller and put code:<\/p>\n\n\n\n<p><strong>app\/Http\/Controllers\/ProductController.php<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/31e3b6c921c797287c4b739463136c27.js\"><\/script>\n\n\n\n<p><strong>Step 8: Create new Routes<\/strong><\/p>\n\n\n\n<p>In this step, we are doing from scratch so we will add three routes, one for display data and another for delete request, then third for remove all selected data. So you have to simply add three new routes in your laravel application.<\/p>\n\n\n\n<p><strong>routes\/web.php<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/10\/route-1024x556.png\" alt=\"\" class=\"wp-image-19098\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Step 9 \u2013 Set Data in View File in Add Product details<\/h3>\n\n\n\n<p>This is the last step in the Laravel 5.8 Crud application, and in this step, we have to set data in the view file which has been store under&nbsp;the resources\/views&nbsp;folder because this view file has received data from the controller method, so here we have to set data in the view file. Below you can file all view file which has been used in the Crud application, and you can also find how data have been set and how to make a form in Add Product details<\/p>\n\n\n\n<p><strong>resources\/views\/sorces.blade.php<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/82c52f6f045cb3e3fbf55970017a3270.js\"><\/script>\n\n\n\n<p><strong>Step 10: Add Blade File<\/strong><\/p>\n\n\n\n<p>In last step, we will create products.blade.php file and write code of jquery for delete and delete all function. So let&#8217;s create products.blade.php file and put bellow code:<\/p>\n\n\n\n<p><strong>resources\/views\/products.blade.php<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/749759c24f0421e398ac2c590f19ce1b.js\"><\/script>\n\n\n\n<p><strong>Step 10: Add Blade File<\/strong><\/p>\n\n\n\n<p><strong>create() \u2013&nbsp;<\/strong>This method has been used for load&nbsp;the <strong>create.blade.php<\/strong>&nbsp;file. In this file, users can find a form for insert new records and filling data insert data requests will be sent to the store() method of CrudsController.php controller class.<\/p>\n\n\n\n<p><strong>resources\/views\/create.blade.php<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/15d7cd034d54f1acf8abec5bade63cad.js\"><\/script>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_79430\"><iframe loading=\"lazy\"  id=\"_ytid_32796\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_79430\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>It&#8217;s almost need to give feature for remove multiple records using checkbox, if you are developing e-commerce application or any big web application then you must give feature to delete&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5201,6305],"tags":[],"class_list":["post-19057","post","type-post","status-publish","format-standard","hentry","category-laravel","category-jquery"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/19057","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=19057"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/19057\/revisions"}],"predecessor-version":[{"id":24519,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/19057\/revisions\/24519"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=19057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=19057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=19057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}