Site Admin
Site Admin Founder of MeaningArticles
1775 Views

Laravel Delete Multiple Records Using Checkbox

Hello Dev.
In this article i will display you how to delete multiple data the use of checkbox in laravel or delete multiple rows in laravel using jquery.

oftentimes we've got multiple data in database and whenever we want to delete data it's time consuming to delete data one by one. So, in this case we will delete multiple data using checkbox or we are able to delete all records at a equal time.

So, let's start and follow below steps.


Step 1: Install Laravel

Install laravel application. if you don't have laravel application in your system. so copy below command and create laravel application

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


Step 2: Database Setup

In this step, we will configure database for example database name, username, password etc. for our demo of laravel. So, open .env file and fill all details like as bellow.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database name(blog)
DB_USERNAME=database username(root)
DB_PASSWORD=database password(NULL)


Step 3: Add Dummy Records

Now,  Run below command in your terminal to add multiple dummy user's data , it will inserted 300 records automatically using Tinker.

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


Step 4: Create Controller

After executed step 3 now create controller on this path app\Http\Controllers\DeleteUserController.php and add below command.

<?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();
    }
}


Step 5: Add Route

Now Add route 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');


Step 6: Create Blade File

We need to create blade file for view output. So, create new blade file in this path contactlist\resources\views\delete_multiple_user.blade.php and add below code.

<!DOCTYPE html>
<html>
<head>
    <title>How to Delete Multiple Records in Laravel - meaningarticle.com</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 - meaningarticle.com</h1>
    <form method="post" action="{{url('multipleusersdelete')}}">
        {{ csrf_field() }}
        <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>

Now, We are done with our code

So, copy below command and run in terminal.

php artisan serve

After that run this project in your browser.

http://localhost:8000/contactlist

i'm hoping it assist you to, thanks for visit my article if you like my article then proportion together with your friend and social platform.