How to delete the data from Database using Laravel 11
In this tutorial, we will learn How to delete the data from the database using Laravel 11. To delete data in Laravel 11, follow these steps:
Files created for this tutorial
fetch.blade.php (resources/views/fetch.blade.php)
deleteController.php (app/Http/Controllers/deleteController.php)
web.php (routes/web.php)
The User Data looks like before the delete.

Step 1: Create the Fetch View
Already created in the previous tutorial
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 |
@if(session('success')) <div class="alert alert-success">{{ session('success') }}</div> @endif @if(session('error')) <div class="alert alert-danger">{{ session('error') }}</div> @endif <table class="table table-striped table-hover table-bordered"> <thead> <tr> <th>#</th> <th>First Name</th> <th>Last Name</th> <th>Email id</th> <th>Mobile No</th> <th>Address</th> <th>City</th> <th>State</th> <th>Action</th> </tr> </thead> <tbody> @foreach ($data as $id => $user ) <tr> <td>1</td> <td>{{ $user->firstName }}</td> <td>{{ $user->lastName }}</td> <td>{{ $user->emailId }}</td> <td>{{ $user->mobileNumber }}</td> <td>{{ $user->address }}</td> <td>{{ $user->city }}</td> <td>{{ $user->state }}</td> <td> <a class="btn btn-primary btn-sm" href="{{ route('update', $user->id) }}">Edit</a></td> </tr> @endforeach </tbody> </table> |
Step 2: Create a controller for data deleting.
1 |
php artisan make:controller deleteController |
Add logic to delete data in the controller (app/Http/Controllers/deleteController.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; use Illuminate\Http\Request; class deleteController extends Controller { public function deleteUser(string $id){ $user=DB::table('tblusers') ->where('id', $id) ->delete(); if($user){ return redirect()->route('fetch')->with('success', 'Data deleted.'); } else{ return redirect()->route('fetch')->with('Something went wrong . Please Try again.'); } } } |
How to run the Script
1. Download the project zip file
2. Extract the file and copy insert-app
folder
3. Paste inside root directory (for xampp xampp/htdocs, for wamp wamp/www, for lamp var/www/Html)
4.Open PHPMyAdmin (http://localhost/phpmyadmin)
5. Create a database with the name userdb
6. Import userdb.sql
file(given inside the zip package in SQL file folder)
7. Run these command
PS C :\> cd xampp/htdocs/insert-app
PS C:\xampp\htdocs\insert-app> php artisan serve
8. After that open the browser and run the script