To delete records we can use DB facade with the delete method. To do so follow the below steps one by one:
- Step 1: Create Controller UserController by executing this command.
php artisan make:controller UserController

- Step 2: We can delete records in two ways.
First Method: The first is to delete direct using database command. Write following Code in App/Http/Controllers/UserController.php
<?phpnamespaceApp\Http\Controllers;useIlluminate\Http\Request;useDB;classUserControllerextendsController {publicfunctionindex(){$users= DB::select('SELECT * FROM users');returnview('user', ['users'=>$users]);}publicfunctiondestroy($id){DB::delete('DELETE FROM users WHERE id = ?', [$id]);echo("User Record deleted successfully.");returnredirect()->route('users.index');}}chevron_rightfilter_noneSecond Method: The second way is to delete using the Laravel delete Function and User Model (Easy one).
<?phpnamespaceApp\Http\Controllers;useIlluminate\Http\Request;useApp\User;classUserControllerextendsController {publicfunctionindex(){$users= User::All();returnview('user', ['users'=>$users]);}publicfunctiondestroy($id){$user= User::where('id',$id)->firstorfail()->delete();echo("User Record deleted successfully.");returnredirect()->route('users.index');}}chevron_rightfilter_none - Step 3: Implementation or Driver Code and create web routes for implementation of the above code in routes/web.php
<?phpRoute::get('/user','UserController@index')->name('users.index');Route::delete('/user/{id}','UserController@destroy')->name('users.destroy');?>chevron_rightfilter_none - Step 4: Create a View File from where we display our users in resources/views directory name user.blade.php. Write following HTML code.
<!DOCTYPE html><html><head><title>Users Record</title><styletype="text/css">table {color: #333;font-family: sans-serif;width: 640px;border-collapse: collapse;border-spacing: 0;}td,th {border: 1px solid #CCC;height: 30px;}th {background: #F3F3F3;font-weight: bold;}td {background: #FAFAFA;text-align: center;}</style></head><body><table><tr><td>ID</td><td>Name</td><td>Email</td><td>Delete</td></tr>@foreach ($users as $user)<tr><td>{{ $user->id }}</td><td>{{ $user->name }}</td><td>{{ $user->email }}</td><td><ahref="{{ route('users.index') }}"onclick="event.preventDefault();document.getElementById('delete-form-{{$user->id}}').submit();">Delete</a></td><formid="delete-form-{{$user->id}}"+ action="{{route('users.destroy', $user->id)}}"method="post">@csrf @method('DELETE')</form></tr>@endforeach</table></body></html>chevron_rightfilter_none - Step 5: Start the server by executing php artisan:serve command and go to http://localhost:8000/user and the output will be:
- Output: Click on the delete button to get the record deleted. After deleting two records output is:

Recommended Posts:
- Laravel | Artisan Commands to know in Laravel
- Delete the array elements in JavaScript | delete vs splice
- Laravel | View Basics
- Laravel | Controller Basics
- Laravel | CSRF Protection
- Laravel | Migration Basics
- Differences Between Django vs Laravel
- Laravel | Front-end Scaffolding
- Laravel | Directory Structure
- Laravel | Installation and Configuration
- Laravel | Validation Rules
- Laravel | Routing Basics
- Difference Between Laravel and CodeIgniter Framework in PHP
- Laravel | Eloquent Model Basics
- Laravel | MySQL Database Connection
- What are the differences and Similarities Between Lumen and Laravel?
- Laravel | Blade Templates Inheritance
- Laravel | Artisan Console Introduction
- Difference Between Laravel and Ruby on Rails
- Different ways for passing data to view in Laravel
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

