How to delete multiple records in Laravel using checkbox

Hey guys, in this post we will learn how to delete multiple records using checkbox.

  1. Create new Laravel application.

First create new laravel application using artisan command:

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

2. Create Controller

Now create controller for user listing and delete functions.

php artisan make:controller UserController –-resource
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;

class UserController extends Controller
{

    public function viewAllUser(Request $request)
	{
		$list = User::orderby('id', 'desc')->get();
		return view('view_user')->with('list', $list);
	}

    public function deleteMultiRecords(Request $request)
	{
		$id = $request->id;
		foreach ($id as $user) 
		{
			User::where('id', $user)->delete();
		}
        return redirect('/users');
	}

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

3. Create Blade

Create a blade file inside views directory

resources\views\view_user.blade.php

<!DOCTYPE html>
<html>
<head>
	<title>How to Delete Multiple Records using Checkbox in Laravel - Blogshub.co.in</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 using Checkbox in Laravel - Blogshub.co.in </h1>
	<form method="post" action="{{url('deleteMultiRecords')}}">
		{{ csrf_field() }}
		<br>
		<input class="btn btn-success" type="submit" name="submit" value="Delete"/>
		<br><br>
		<table class="table-bordered table-striped" width="50%">
			<thead>
				<tr>
                    <th class="text-center">Sr</th>
                    <th class="text-center"> <input type="checkbox" id="checkAll"> Select All</th>
					<th class="text-center">Name</th>
					<th class="text-center">Email</th>

					
				</tr>
			</thead>
			<tbody>
				<?php
				$i=1;
				foreach ($list as $key => $value) {
					$name = $list[$key]->name;
                    $email = $list[$key]->email;
					?>
					<tr>
                        <td class="text-center">{{$i}}</td>
                        <td class="text-center"><input name='id[]' type="checkbox" id="checkItem" 
                         value="<?php echo $list[$key]->id; ?>">
						<td class="text-center">{{$name}}</td>
                        <td class="text-center">{{$email}}</td>
						</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>

4. Create routes in web.php

routes\web.php

<?php

use Illuminate\Support\Facades\Route;
use App\http\controllers\UserController;

Route::get('/', function () {
    return view('welcome');
});

Route::get('/users', [UserController::class, 'viewAllUser']);
Route::post('/deleteMultiRecords', [UserController::class, 'deleteMultiRecords']);

5. Configure database credentials using .env file

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=multiple_delete
DB_USERNAME=root
DB_PASSWORD=

6. Run migrate command

php artisan migrate

7. Add dummy data

php artisan tinker
User::factory()->count(100)->make();

8. Run application

php artisan serve

Keep Learning 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *