On this tutorial, we’ll study Find out how to delete the information from the database utilizing Laravel 11. To delete information in Laravel 11, comply with these steps:
Information created for this tutorial
fetch.blade.php (sources/views/fetch.blade.php)
deleteController.php (app/Http/Controllers/deleteController.php)
internet.php (routes/internet.php)
The Consumer Information appears to be like like earlier than the delete.

Step 1: Create the Fetch View
Already created within the earlier tutorial
@if(session(‘error’))
<div class=”alert alert-danger”>{{ session(‘error’) }}</div>
@endif
<desk class=”desk table-striped table-hover table-bordered”>
<thead>
<tr>
<th>#</th>
<th>First Identify</th>
<th>Final Identify</th>
<th>Electronic mail id</th>
<th>Cellular No</th>
<th>Deal with</th>
<th>Metropolis</th>
<th>State</th>
<th>Motion</th>
</tr>
</thead>
<tbody>
@foreach ($information as $id => $consumer )
<tr>
<td>1</td>
<td>{{ $user->firstName }}</td>
<td>{{ $user->lastName }}</td>
<td>{{ $user->emailId }}</td>
<td>{{ $user->mobileNumber }}</td>
<td>{{ $user->handle }}</td>
<td>{{ $user->metropolis }}</td>
<td>{{ $user->state }}</td>
<td> <a category=”btn btn-primary btn-sm” href=”https://phpgurukul.com/how-to-delete-the-data-from-database-using-laravel-11/{{ route(“replace’, $user->id) }}”>Edit</a></td>
</tr>
@endforeach
</tbody>
</desk>
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 <desk class=“desk table-striped table-hover table-bordered”> <thead> <tr> <th>#</th> <th>First Identify</th> <th>Final Identify</th> <th>Electronic mail id</th> <th>Cellular No</th> <th>Deal with</th> <th>Metropolis</th> <th>State</th> <th>Motion</th> </tr> </thead> <tbody> @foreach ($information as $id => $consumer ) <tr> <td>1</td> <td>{{ $consumer->firstName }}</td> <td>{{ $consumer->lastName }}</td> <td>{{ $consumer->emailId }}</td> <td>{{ $consumer->mobileNumber }}</td> <td>{{ $consumer->handle }}</td> <td>{{ $consumer->metropolis }}</td> <td>{{ $consumer->state }}</td> <td> <a class=“btn btn-primary btn-sm” href=“https://phpgurukul.com/how-to-delete-the-data-from-database-using-laravel-11/{{ route(“replace’, $user->id) }}”>Edit</a></td> </tr> @endforeach
</tbody> </desk> |
Step 2: Create a controller for information deleting.
php artisan make:controller deleteController |
Add logic to delete information within the controller (app/Http/Controllers/deleteController.php)
class deleteController extends Controller
{
public operate deleteUser(string $id){
$consumer=DB::desk(‘tblusers’)
->the place(‘id’, $id)
->delete();
if($consumer){
return redirect()->route(‘fetch’)->with(‘success’, ‘Information deleted.’);
} else{
return redirect()->route(‘fetch’)->with(‘One thing went incorrect . Please Attempt once more.’);
}
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php namespace AppHttpControllers; use IlluminateAssistFacadesDB; use IlluminateHttpRequest;
class deleteController extends Controller { public operate deleteUser(string $id){ $consumer=DB::desk(‘tblusers’) ->the place(‘id’, $id) ->delete(); if($consumer){ return redirect()->route(‘fetch’)->with(‘success’, ‘Information deleted.’); } else{ return redirect()->route(‘fetch’)->with(‘One thing went incorrect . Please Attempt once more.’); } } } |
Find out how to run the Script
1. Obtain the venture zip file
2. Extract the file and duplicate insert-app
folder
3. Paste inside root listing (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 identify userdb
6. Import userdb.sql
file(given contained in the zip package deal 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
Obtain Full supply code(Delete Information Utilizing Laravel11)
Dimension: 27.5 MB
Model: V 1.0