Laravel 10 Delete Multiple Records Using Checkbox

Websolutionstuff | Mar-03-2023 | Categories : Laravel PHP MySQL

In this article, we will see laravel 10 delete multiple records using the checkbox. Here, we will learn about how to delete multiple records using a checkbox in laravel 10.

In the table, we have multiple records at that time we need to remove multiple records or need to remove the selected records from the database. So, that time it can easily remove records from the database.

So, let's see delete multiple records in laravel 10, how to delete a particular row in laravel 10, and how to delete multiple rows in laravel 10.

Step 1: Install Laravel 10

In this step, we will install laravel 10 using the following command.

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

 

 

Step 2: Configure Database

Now, we will configure database details as in the below example.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_10_checkbox_example
DB_USERNAME=root
DB_PASSWORD=root

 

Step 3: Add Dummy Records Using Tinker

In this step, we will add some dummy records into the database to perform multiple records delete from the database. So, run the following command and create dummy records.

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

 

Step 4: Create Controller

In this step, we will create UserController using the following command. So, add the below code to that file.

php artisan make:controller UserController

app/Http/Controllers/UserController.php

<?php

namespace App\Http\Controllers;

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

class UserController extends Controller
{
	public function index(Request $request)
	{
		$list = User::orderby('id', 'desc')->get();
		
    	return view('index')->with('list', $list);
	}

	public function deleteMultipleUsers(Request $request)
	{
		$id = $request->id;

		foreach ($id as $user) 
		{
		  User::where('id', $user)->delete();
		}
		return back();
	}
}

 

Step 5: Add Route

In this step, we will add routes to the web.php file.

routes/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;

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

Route::controller(UserController::class)->group(function () {
    Route::get('index', 'index');
    Route::post('delete-multiple-user', 'deleteMultipleUsers')->name('deleteMultipleUsers');
});

 

 

Step 6: Create Blade File

Now, we will create an index.blade.php file. So, add the following code to that file.

resources/views/index.blade.php

<!DOCTYPE html>
<html>
<head>
	<title>Laravel 10 Delete Multiple Records Using Checkbox - Websolutionstuff</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
	<h1>Laravel 10 Delete Multiple Records Using Checkbox - Websolutionstuff</h1>
	<form method="post" action="{{route('deleteMultipleUsers')}}">
		{{ csrf_field() }}
		<br>
		<input class="btn btn-success" type="submit" name="submit" style="float: right;" value="Delete All Users"/>
		<br><br>
		<table class="table-bordered table-striped" width="50%">
			<thead>
				<tr>
					<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>
				</tr>
			</thead>
			<tbody>								
				@foreach ($list as $key => $value)					
					<tr>
						<td class="text-center">{{$key}}</td>
						<td class="text-center">{{$value->name}}</td>
						<td class="text-center"><input name='id[]' type="checkbox" id="checkItem" 
                         value="{{$value->id;}}"></td>
					</tr>
				@endforeach						
			</tbody>
		</table><br>
	</form>
</body>
</html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>		
<script language="javascript">
	$("#checkAll").click(function () {
		$('input:checkbox').not(this).prop('checked', this.checked);
	});
</script>

 


You might also like:

Recommended Post
Featured Post
Laravel 8 CRUD Operation Example
Laravel 8 CRUD Operation Examp...

In this article, we will see the laravel 8 crud operation. As you know Laravel 8 has already been officially released an...

Read More

Sep-16-2020

PHP 8.3: Release Date and New Features
PHP 8.3: Release Date and New...

Welcome web development enthusiasts! Here, we are going to talk about the much-awaited PHP 8.3. It is packed with a m...

Read More

Jan-12-2023

How to Create ZIP Archive File in Laravel 11 Example
How to Create ZIP Archive File...

Hello, laravel web developers! In this article, we'll see how to create a zip archive file in laravel 11. You can cr...

Read More

Jul-17-2024

MilesWeb Review: Why Go for Its Shared Hosting?
MilesWeb Review: Why Go for It...

For finding the right web hosting provider, you need to do a lot of research. There are hundreds of web hosting provider...

Read More

Nov-12-2021