In this tutorial we are going to delete multiple rows using checkbox in laravel using eloquent and in php using procedural php programming.
This gets really helpful when you have to delete multiple records from database or while removing the list of post in your database.
It saves a tons of time and with the help of eloquent, it is much easier to perform this task.
Lets get started
What are we going to do ?
- First we are going to perform multiple delete in laravel using eloquent.
- We are going to do same in core php as well.
How to Delete Multiple Records Using Eloquent in Laravel
Setup your laravel app, model and the migration. I have just using two fields for now to meet the requirements of this tutorial.
I created the model name tbl_data
and the tbl_datas
migration have been created which my table name is going to be. I have used faker to insert the dummy data. If you want take look at my table take a look below :-
If you want to use the copy of my migration then copy these code below :-
1 2 3 4 5 6 7 8 9 |
public function up() { Schema::create('tbl_datas', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->string('description'); $table->timestamps(); }); } |
Now lets setup routes and send the data to view. Go to your routes/web.php
and write these codes below:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Route::get('/', function () { $data = DB::table('tbl_datas')->get(); return view('form',['data'=>$data]); }); Route::post('bulk-delete',function(Request $request) { $all_data = $request->except('_token'); foreach($all_data as $id) { tbl_data::where('id',$id)->delete(); } return redirect()->back(); }); |
Make sure to use model and request class on top of your routes/web.php
so that you can use its functions.
1 2 |
use App\tbl_data; use Illuminate\Http\Request; |
Now lets create the form inside the resources/view
folder and name it as form.blade.php
. I have used bootstrap 4 CDN to quickly make the elegant form for this tutorial. Check below and copy and paste if you want.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Multiple Delete Options in Laravel</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css"> <style> * { margin:0px auto! important; margin-top: 10px; } </style> </head> <body> <div class="container"> <div class="row"> <div class="col-md-10"> <div class="table-responsive"> <form action="{{ url('bulk-delete') }}" method="POST"> {!! csrf_field() !!} <button type="submit" class="btn btn-primary" id="bulk-delete" style="display:none">Delete</button> <table class="table table-hover table-condensed table-striped"> <thead> <td><input type="checkbox" id="selectall" class="checked" /></td> <th>S.N</th> <th>Title</th> <th>Description</th> </thead> <tbody> @php($i=1) @foreach($data as $d) <tr> <td><input type="checkbox" onClick="checkbox_is_checked()" name="id[]" value="{{$d->id}}" class="check-all"></td> <td>{{$i}}</td> <td>{{ $d->title }}</td> <td>{{ $d->description }}</td> </tr> @php($i++) @endforeach </tbody> </table> </form> </div> </div> </div> </div> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script> </body> </html> |
In above view file (form.blade.php)
. We have created one main checkbox to use as bulk select all the checkboxes. In order to check all checkboxes in the form while the main checkbox is clicked so, we need to trigger java script event for it. Lets create one.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$(function () { $("#selectall").click(function () { if ($("#selectall").is(':checked')) { $("input[type=checkbox]").each(function () { $(this).attr("checked", true); }); $("#bulk-delete").show(); } else { $("input[type=checkbox]").each(function () { $(this).attr("checked", false); }); $("#bulk-delete").hide(); } }); }); |
We only want to show delete button when we have any one of the checkbox is checked so we are going to use jQuery hide() and show()
function to trigger it.
1 2 3 4 5 6 7 8 9 10 11 |
function checkbox_is_checked() { if ($(".check-all:checked").length > 0) { $("#bulk-delete").show(); } else { $("#bulk-delete").hide(); } } |
This is it for laravel, try php artisan serve
and try to delete the multiple records below. If it didnot worked make sure to write your problem in comments. Lets now move to next part.
How to Delete Multiple Rows Using Checkboxes in PHP Using Javascript ?
So in this tutorial we are going to use the same database that we used in our laravel tutorial. We are going to write core php code so that we could fit it in single page and everybody could understand.
We are going to use same html form and javascript functions that we have used earlier with only some of the changes. Take a look for the changes below :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
<?php //Connect to the database $conn = mysqli_connect("localhost","root","","multiple"); // Preparing the query $sql = "SELECT * from tbl_datas"; // Executing the query $list = mysqli_query($conn,$sql); ?> <div class="container"> <div class="row"> <div class="col-md-10"> <div class="table-responsive"> <form action="" method="POST"> <button type="submit" class="btn btn-primary" id="bulk-delete" style="display:none">Delete</button> <table class="table table-hover table-condensed table-striped"> <thead> <th><input type="checkbox" id="selectall" class="checked" /></th> <th>S.N</th> <th>Title</th> <th>Description</th> </thead> <tbody> <?php $i=1; ?> <?php while($d = mysqli_fetch_assoc($list)): ?> <tr> <td><input type="checkbox" onClick="checkbox_is_checked()" name="id[]" value="<?php echo $d['id']; ?>" class="check-all"></td> <td><?php echo $i; ?></td> <td><?php echo $d['title']; ?></td> <td><?php echo $d['description']; ?></td> </tr> <?php $i++; endWhile; ?> </tbody> </table> </form> </div> </div> </div> </div> <script> $(function () { $("#selectall").click(function () { if ($("#selectall").is(':checked')) { $("input[type=checkbox]").each(function () { $(this).attr("checked", true); }); $("#bulk-delete").show(); } else { $("input[type=checkbox]").each(function () { $(this).attr("checked", false); }); $("#bulk-delete").hide(); } }); }); function checkbox_is_checked() { if ($(".check-all:checked").length > 0) { $("#bulk-delete").show(); } else { $("#bulk-delete").hide(); } } </script> |
Now we have obtained the list of records from the database time to write the multiple delete code.
Go to top where you have made the database connection in your php file and just below it copy paste these code below :-
1 2 3 4 5 6 7 8 9 |
// Checking if there is incoming post request [User pushed the delete button] if(isset($_POST)) { // check the number od checkboxes and their values and execute the query inside the for loop. foreach($_POST['id'] as $val) { $sql = "delete from tbl_datas where id = $val"; mysqli_query($conn,$sql); } } |
Now you have successfully created code to delete multiple records using checkboxes in php as well as laravel. If this tutorial helped your subscribe or like our facebook page to get more tutorials ahead.
Leave a Reply