Laravel | Delete Records

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');}}Second 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');}} - 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');?> - 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> - 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:




