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.
![Laravel-Data-delete](https://phpgurukul.com/wp-content/uploads/2025/02/Laravel-Data-delete-1024x481.png)
Step 1: Create the Fetch View
Already created in the previous tutorial
@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.
php artisan make:controller deleteController
Add logic to delete data in the controller (app/Http/Controllers/deleteController.php)
<?php namespace AppHttpControllers; use IlluminateSupportFacadesDB; use IlluminateHttpRequest; 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:xampphtdocsinsert-app> php artisan serve
8. After that open the browser and run the script
The post How to delete the data from Database using Laravel 11 appeared first on PHPGurukul.
Source: Read MoreÂ