Delete query in laravel

In Laravel we Can delete a record in database using delete method.  syntax for delete a record in laravel

Syntaxint delete(string $query, array $bindings = array())
Parameters
  • $query(string) − query to execute in database
  • $bindings(array) − values to bind with queries
Returnsint
DescriptionRun a delete statement against the database.

Simple query to delete a record in laravel

DB::table('users')->delete($id);

The above is identical to this:

DB::table('users')->where('id', $id)->delete();

if this code not works then we will explain step by step process to delete a record

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 −

Step 3 − Copy the following code to file

app/Http/Controllers/StudDeleteController.php

app/Http/Controllers/StudDeleteController.php

$users]);
   }
   public function destroy($id) {
      DB::delete('delete from student where id = ?',[$id]);
      echo "Record deleted successfully.
"; echo 'Click Here 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


   
   
      View Student Records
   
   
   
      
         @foreach ($users as $user)
         
         @endforeach
      
ID Name Edit
{{ $user->id }} {{ $user->name }} Delete

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

app/Http/routes.php

Route::get('delete-records','[email protected]');
Route::get('delete/{id}','[email protected]');

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

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.

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.