Delete Multiple Records using Checkbox in Laravel
In this section, we are going to use Laravel to delete more than one record by using the checkbox. If we want to develop a huge web application or an e-commerce application, we should provide the feature to remove more than one record.
Now we will describe the use of a checkbox to delete multiple records in various versions of laravel applications like 5, 6, 7, and 8. Here we can delete a single record or more than one record. In our example, we will create a table named "products", which will contain the created_at, details, updated_at columns, name, and id. We will also add dummy records by using the mysql query. In order to delete all records by selecting all checkboxes, we will use jquery. For this, we should follow the following steps:
In the first step, we will create a product table with dummy records. For this, we will create a "product" table. After that, we will create dummy records by running mysql query. We can use a migration to create the product table, and then we will use seeder to create some dummy records. In the following example, we will simply use the sql query.
Dummy record query:
In this step, we will Create new routers. For this, we will add tree routers. The first router will be created for displaying data. The second router will be created for deleting the request. The third router will be created to remove all selected data. So we will use our laravel application and add these three new routers.
In this step, we will Add a product controller. For this, we will create a new ProductController file, which is used to handle the request created by the above three routers. We will use this controller to create three methods, i.e., deleteAll(), destroy(), index(). These methods are used to handle the request of the router. Now we will create a new controller and then put the following code:
In this step, we will Add blade files. For this, we will create product.blade.php file. For the delete all and delete function, we will write the below code of jquery. Now we will create product.blade.php file and then put the following code.
Now our above code is ready to run. In order to run the above code quickly, we will use the following command:
Now we can use our browser to open the below url:
After open this, we can see the following output: