How to Delete Multiple Records using Checkbox in Laravel? (ok)

https://www.itsolutionstuff.com/post/how-to-delete-multiple-records-using-checkbox-in-laravel-5-example.html

C:\xampp\htdocs\reset\routes\web.php

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\ProductController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
    return view('welcome');
});
Auth::routes();
Route::get('/home', [HomeController::class, 'index'])->name('home');
Route::get('myproducts',  [ProductController::class, 'index']);
Route::delete('myproducts/{id}',  [ProductController::class, 'destroy']);
Route::delete('myproductsDeleteAll',  [ProductController::class, 'deleteAll']);

C:\xampp\htdocs\reset\app\Http\Controllers\ProductController.php

C:\xampp\htdocs\reset\resources\views\products.blade.php

How to Delete Multiple Records using Checkbox in Laravel?

By Hardik Savani July 18, 2017 Category : PHP Laravel Bootstrap jQuery MySql AjaxPlayUnmuteLoaded: 1.17%FullscreenVDO.AIIt'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.

So in this post, i will let you know how to delete multiple records with checkbox in laravel 5, laravel 6, laravel 7, laravel 8 and laravel 9 application. here i also give multiple delete and you can also delete single records. Few days ago i posted for confirmation before delete record, so you can read from here : Laravel 5 - Confirmation before delete record from database example.

In this example, i simply created "products" 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.

Preview:

Step 1: Create products Table with Dummy Records

Here, you have to create "products" table then you can run mysql query for dummy records. You can create products table using migration and then also create some dummy records using seeder. So now i just simple sql query.

Dummy Records Query:

Step 2: Create new Routes

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.

routes/web.php

Read Also: How to add Delete cascade to existing column in Laravel 5, without remove column?

Step 3: Add ProductController

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's create new controller and put code:

app/Http/Controllers/ProductController.php

Step 4: Add Blade File

In last step, we will create products.blade.php file and write code of jquery for delete and delete all function. So let's create products.blade.php file and put bellow code:

resources/views/products.blade.php

Now we are ready to run our example so run bellow command for quick run:

Now you can open bellow url on your browser:Read Also: Laravel 5 email verification with activation code example

I hope it can help you....

Last updated

Was this helpful?