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
<?php
namespace
App\Http\Controllers;
use
Illuminate\Http\Request;
use
DB;
class
UserController
extends
Controller {
      Â
public
function
index()Â
      Â
{
         Â
$users
= DB::select(
'SELECT * FROM users'
);
         Â
return
view(
'user'
, [
'users'
=>
$users
]);
      Â
}
      Â
public
function
destroy(
$id
)Â
      Â
{
         Â
DB::
delete
(
'DELETE FROM users WHERE id = ?'
, [
$id
]);
         Â
echo
(
"User Record deleted successfully."
);
         Â
return
redirect()->route(
'users.index'
);
      Â
}
}
Second Method: The second way is to delete using the Laravel delete Function and User Model (Easy one).
<?php
namespace
App\Http\Controllers;
use
Illuminate\Http\Request;
use
App\User;
class
UserController
extends
Controller {
      Â
public
function
index()Â
      Â
{
         Â
$users
= User::All();
         Â
return
view(
'user'
, [
'users'
=>
$users
]);
      Â
}
      Â
public
function
destroy(
$id
)Â
      Â
{
         Â
$user
= User::where(
'id'
,
$id
)->firstorfail()->
delete
();
         Â
echo
(
"User Record deleted successfully."
);
         Â
return
redirect()->route(
'users.index'
);
      Â
}
}
- Step 3: Implementation or Driver Code and create web routes for implementation of the above code in routes/web.php
<?php
Route::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
>
   Â
<
style
type
=
"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
><
a
href
=
"{{ route('users.index') }}"
ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
onclick="event.preventDefault();
                   Â
document.getElementById(
                     Â
'delete-form-{{$user->id}}').submit();">
                Â
DeleteÂ
               Â
</
a
>
           Â
</
td
>
           Â
<
form
id="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: