Hi Developers,
Lets see How To Delete Multiple Records Using Checkbox In Laravel. While the user wanted to delete multiple records it will be useful to use checkbok for selecting the records that should be deleted it will be more time efficient way rather than deleting every single record so lets see How To Delete Multiple Records Using Checkbox In Laravel
Lets create a new Laravel Project
laravel new delete_multiple
Lets configure the database details in .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=delete_multiple
DB_USERNAME=
DB_PASSWORD=
Now, Run below command in your terminal to add multiple dummy user's data , it will inserted 200 records automatically. hence in laravel the factory file for users table was provided by default.
php artisan tinker
factory(App\User::class, 200)->create();
Lets create a new controller by using the following command
php artisan make:controller DeleteUserController
place the following code in app\Http\Controllers\DeleteUserController.php
<?php
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();
}
}
Lets add routes in routes/web.php
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::get('contactlist', 'DeleteUserController@contactlist');
Route::post('multipleusersdelete', 'DeleteUserController@multipleusersdelete');
Lets create a new Laravel Project blade file for view output , So create new blade file in this path
resources\views\delete_multiple_user.blade.php
<!DOCTYPE html>
<html>
<head>
<title>How to Delete Multiple Records in Laravel - ITSolutionsGuides</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>How to Delete Multiple Records in Laravel - ITSolutionsGuides</h1>
<form method="post" action="{{url('multipleusersdelete')}}">
@csrf
<br>
<input class="btn btn-success" type="submit" name="submit" 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>
<?php
$i=1;
foreach ($list as $key => $value) {
$name = $list[$key]->name;
?>
<tr>
<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; ?>">
</tr>
<?php $i++; }?>
</tbody>
</table>
<br>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</script>
<script language="javascript">
$("#checkAll").click(function () {
$('input:checkbox').not(this).prop('checked', this.checked);
});
</script>
</body>
</html>
Run the following command
php artisan serve
We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.
Be the first to know about releases and tutorial news and solutions.
We care about your data in our privacy policy.
ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.
Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides