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

Example

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

app/Http/Controllers/StudDeleteController.php

app/Http/Controllers/StudDeleteController.php

<?php

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.
resources/views/stud_delete_view.blade.php

<html>
   
   <head>
      <title>View Student Records</title>
   </head>
   
   <body>
      <table border = "1">
         <tr>
            <td>ID</td>
            <td>Name</td>
            <td>Edit</td>
         </tr>
         @foreach ($users as $user)
         <tr>
            <td>{{ $user->id }}</td>
            <td>{{ $user->name }}</td>
            <td><a href = 'delete/{{ $user->id }}'>Delete</a></td>
         </tr>
         @endforeach
      </table>
   </body>
</html>

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

app/Http/routes.php

Route::get('delete-records','StudDeleteController@index');
Route::get('delete/{id}','StudDeleteController@destroy');

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);
      $company->delete();
      return response()->json([
        'message' => 'Data deleted successfully!'
      ]);

}

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

//sweet alert cdn
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></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 }}">
   Delete
</a>

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

$(document).ready(function () {

    $("#deleteCompany").click(function(e){

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

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

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

            $("#success").html(response.message)

            Swal.fire(
              'Remind!',
              'Company deleted successfully!',
              'success'
            )
        }
     });
      return false;
   });

});

Hope it can help you.

#laravel #php #web-development

How to Delete Data Records in Laravel 7
117.85 GEEK