In this tutorial, we'll explore how to delete multiple records using checkboxes in Laravel 10. This feature is handy when dealing with datasets where users must selectively remove entries from a database.
In this guide, we'll see how to delete multiple records at the same using the checkbox in laravel 10 and jQuery. Also, you can use it in laravel 8/9 and PHP.
So, let's see how to delete multiple records using the checkbox in laravel 10, laravel 10 delete multiple records using the checkbox, and how to delete selected data in laravel 8/9/10.
Begin by installing Laravel 10 with the following command:
composer create-project --prefer-dist laravel/laravel laravel_10_checkbox_example
Next, configure your database details in the .env
file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_10_checkbox_example
DB_USERNAME=root
DB_PASSWORD=
To populate your database with dummy records, run the following command:
php artisan tinker
factory(App\User::class, 100)->create();
Generate a new controller named UserController
with the command:
php artisan make:controller UserController
Then, add the following code to the UserController.php
file:
<?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)
{
$ids = $request->id;
foreach ($ids as $id) {
User::where('id', $id)->delete();
}
return back();
}
}
In the routes/web.php
file, add the following routes:
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');
});
Now, create a blade file named index.blade.php
in the resources/views
directory and populate it with the following code:
<!DOCTYPE html>
<html>
<head>
<title>Delete Multiple Records Using Checkbox in Laravel 10 - Vidvatek</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>Delete Multiple Records Using Checkbox in Laravel 10 - Vidvatek</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" class="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:
- Read Also: How to Add Soft Delete in Laravel 10
- Read Also: Laravel 10 React JS Pagination using Vite
- Read Also: How to Create Laravel 10 Vue 3 Authentication
- Read Also: How to Delete Relationship Records in Laravel 10