Laravel - Delete Records


We can delete the record using the DB facade with the delete method. The syntax of delete method is shown in the following table.

Syntax int delete(string $query, array $bindings = array())
  • $query(string) − query to execute in database
  • $bindings(array) − values to bind with queries
Returns int
Description Run a delete statement against the database.


Step 1 − Execute the below command to create a controller called StudDeleteController.

php artisan make:controller StudDeleteController --plain

Step 2 − After successful execution, you will receive the following output −

Delete Records

Step 3 − Copy the following code to file




namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class StudDeleteController extends Controller {
   public function index() {
      $users = DB::select('select * from student');
      return view('stud_delete_view',['users'=>$users]);
   public function destroy($id) {
      DB::delete('delete from student where id = ?',[$id]);
      echo "Record deleted successfully.<br/>";
      echo '<a href = "/delete-records">Click Here</a> to go back.';

Step 4 − Create a view file called

resources/views/stud_delete_view.blade.php and copy the following code in that file.


      <title>View Student Records</title>
      <table border = "1">
         @foreach ($users as $user)
            <td>{{ $user->id }}</td>
            <td>{{ $user->name }}</td>
            <td><a href = 'delete/{{ $user->id }}'>Delete</a></td>

Step 5 − Add the following lines in app/Http/routes.php.



Step 6 −The output will appear as shown in the following image.

Name Edit

Step 7 − Click on delete link to delete that record from database. You will be redirected to a page where you will see a message as shown in the following image.

Record Deleted

Step 8 − Click on “Click Here” link and you will be redirected to a page where you will see all the records except the deleted one.