How To Delete Multiple Records Using Checkbox In Laravel

WebSolutionStuff | May-26-2020 | Categories : Laravel PHP jQuery MySQL CSS HTML

In this Example i will show you how to delete multiple records using single checkbox or how to delete multiple seleted records using checkbox.

Many times we have multiple record in database and whenever we want to delete records it is boring and time consuming to delete records one by one, So in this example we can delete multiple selected records on using single checkbox or we can delete all records at a same time, So,Let's start and follow below steps.


Step 1 : Install Laravel

Type the following command in terminal for create new project in your system.

composer create-project --prefer-dist laravel/laravel blog


Step 2 : Database Setup

In second step, we will configure database  for example database name, username, password etc for our demo of laravel. So open .env file and fill all details like as bellow: 

DB_DATABASE=database name(blog)
DB_USERNAME=database username(root)
DB_PASSWORD=database password(NULL)


Step 3: Add Dummy Records Using Tinker

Now,  Run below command in your terminal to add multiple dummy user's data , it will inserted 200 records automatically.

php artisan tinker
factory(App\User::class, 200)->create();


Step 4 : Create Controller DeleteUserController

 After executed step 3 now create controller on this path app\Http\Controllers\DeleteUserController.php and add below command.


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class DeleteUserController extends Controller
	public function contactlist(Request $request)
		$list = User::orderby('id', 'desc')->get();
		return view('delete_multiple_user')->with('list', $list);

	public function multipleusersdelete(Request $request)
		$id = $request->id;
		foreach ($id as $user) 
			User::where('id', $user)->delete();
		return redirect();


Step 5 : Add  Route

Now Add route in routes/web.php


use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');

Route::get('contactlist', '[email protected]');
Route::post('multipleusersdelete', '[email protected]');


Step 6 : Create Blade File For View

 We need to create blade file for view output , So create new blade file in this path contactlist\resources\views\delete_multiple_user.blade.php and add below code.

<!DOCTYPE html>
	<title>How to Delete Multiple Records in Laravel - Websolutionstuff</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="">
	<script src=""></script>
	<h1>How to Delete Multiple Records in Laravel - Websolutionstuff</h1>
	<form method="post" action="{{url('multipleusersdelete')}}">
		{{ csrf_field() }}
		<input class="btn btn-success" type="submit" name="submit" value="Delete All Users"/>
		<table class="table-bordered table-striped" width="50%">
					<th class="text-center">S.No.</th>
					<th class="text-center">User Name</th>
					<th class="text-center"> <input type="checkbox" id="checkAll"> Select All</th>
				foreach ($list as $key => $value) {
					$name = $list[$key]->name;
						<td class="text-center">{{$i}}</td>
						<td class="text-center">{{$name}}</td>
						<td class="text-center"><input name='id[]' type="checkbox" id="checkItem" 
                         value="<?php echo $list[$key]->id; ?>">
						<?php $i++; }?>
			<script src=""></script>
		<script language="javascript">
			$("#checkAll").click(function () {
				$('input:checkbox').not(this).prop('checked', this.checked);

 Now , We are done with our code

So Copy below command and run in terminal.

php artisan serve

After that run this project in your browser.




Featured Post