How to Delete Multiple Records in Laravel?

Keywords :- Delete Multiple Records in Laravel 6.0, Delete Multiple Records in Laravel 5.8, Delete Multiple Records in Laravel 5.7, Delete Multiple Records in Laravel 5.6, Delete Multiple Records in Laravel 5.5, Delete Multiple Records in Laravel 5.4, Delete Multiple Records in Laravel 5.3, Delete Multiple Records in Laravel 5.2, Delete Multiple Records Using Laravel Framework, Delete Multiple Rows Using Laravel

Overview

Step 1:- First you create a blade file (contactlist.blade.php). Then after that you display the list of your contact information with checkbox. After that define the url in the form.

Step 2:- Add the form url to the route file of web.php.

Step 3:- After that you create a controller named deletefileController.php and in this file create query of how to delete your multiple records by Laravel.

resources/views/contactlist.blade.php

<!DOCTYPE html>
<html>
<head>
<title>How to Delete Multiple Records in Laravel?</title>
</head>
<body>
<h1>How to Delete Multiple Records in Laravel?</h1>
<form method="post" action="{{url('multiplerecordsdelete')}}">
{{ csrf_field() }}
<div class="box-body">
<table>
<thead>
<tr>
<th><input type="checkbox" id="checkAll"> Select All</th>
</tr>
<tr>
<th>S.No.</th>
<th>Ckecked box</th>
<th>User Name</th>
</tr>
</thead>
<tbody>
<?php
$i=1;
foreach ($list as $key => $value) {
$name = $list[$key]->name;
?>
<tr>
<td>{{$i}}</td>
<td><input name='id[]' type="checkbox" id="checkItem" value="<?php echo $list[$key]->id; ?>">
<td>{{$name}}</td>
</tr>
<?php $i++; }?>
</tbody>
</table>
</div>
<input type="submit" name="submit" value="Delete All Data"/>
</form>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"> </script>
<script language="javascript">
$("#checkAll").click(function () {
$('input:checkbox').not(this).prop('checked', this.checked);
});
</script>
</body>
</html>

 

routes/web.php

 

Route::get('contactlist', function () {
$list = DB::table('contact')->orderby('id', 'desc')->get();
return view('contactlist')->with('list', $list);
});
Route::post('multiplerecordsdelete', 'deletefileController@multiplerecordsdelete');

 

app/Http/Controllers/deletefileController.php

 

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
use App\Http\Controllers\Controller;
use DB;
use Redirect;
use View;
use File;
class deletefileController extends Controller
{
public function multiplerecordsdelete(Request $req)
{
$id = $req->id;
foreach ($id as $ke) {
DB::table('contact')->where('id', $ke)->delete();
}
return redirect()->back()->with('message','Successfully Delete Your Multiple Selected Records.');;
}
}