In this Laravel 7 tutorial, you’ll learn how to delete data record in Laravel 7

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.

Laravel 7 tutorial


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 −

Laravel 7 tutorial

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.

Laravel 7 tutorial

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.

Laravel 7 tutorial

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.

Laravel 7 tutorial

Delete Record using Ajax Request in Laravel 7

Today i will show you how to delete data using Ajax request in Laravel 7. You have to simply follow few things to make done delete record from database using ajax request.

In this tutorial i will simply delete a column record using ajax request. You will learn from this tutorial about Laravel destroy using ajax request.If you don’t know how to do it, just follow below things.

You know that when we send ajax request to server to delete data, then page won’t refresh but our data will be deleted. How cool, isn’t it? We will use POST request to delete data from database using ajax request.

we will create delete route with controller method(we will write delete row code using database model) and write jquery ajax code with delete post request. we also pass csrf token in jquery ajax request, otherwise it will return error like delete method not allowed.

So let’s start delete row using ajax in laravel tutorial.

Create Route: routes/web.php

Route::delete('/addcompany/{id}', '[email protected]')->name('company.destroy');

Controller Method: app/Http/Controllers/CompanyController.php

public function destroy($id){

      $company = Company::find($id);
      return response()->json([
        'message' => 'Data deleted successfully!'


View Code: resources/views/company.blade.php

//sweet alert cdn
<script src=""></script>

 <a href="{{ route('company.destroy',$element->id) }}" class="btn btn-sm btn-outline-danger py-0" style="font-size: 0.8em;" id="deleteCompany" data-id="{{ $element->id }}">

JS Code: resources/views/company.blade.php

$(document).ready(function () {


    if(!confirm("Do you really want to do this?")) {
       return false;

    var id = $(this).data("id");
    // var id = $(this).attr('data-id');
    var token = $("meta[name='csrf-token']").attr("content");
    var url =;

          url: url.href, //or you can use url: "company/"+id,
          type: 'DELETE',
          data: {
            _token: token,
                id: id
        success: function (response){


              'Company deleted successfully!',
      return false;


Hope it can help you.

#laravel #php #web-development

How to Delete Data Records in Laravel 7
117.85 GEEK