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:

