LARAVEL Tutorial


Laravel 7 Delete Data From Database Table


In the previous tutorial, you have learned how to insert, retrieve/fetch, update data / records into a database table in laravel web application. 

In this delete data into database table laravel tutorial. You will learn how to delete data into the database table in laravel web applications. 

Laravel Delete Data In Database Example

  • Create Routes
  • Create Delete Method in Controller
  • Start Development Server

Create Routes

Now, open your project in a text editor and go to routes/web.php file and update the following routes into your file:

Route::get('delete/{id}','PostController@deleteRecord');

Create Delete Method in Controller

Next step, you need to create one method in the controller for delete data from the database table. 

The deleteRecord() method delete the data from the database table.

So update the below methods in your PostController.php:

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator,Redirect,Response;
use App\Post;

class PostController extends Controller
{  
   
    public function deleteRecord(Request $request, $id)
    {
       
        if(!$id){
           return redirect('/list');
        }

        $check = Post::where('id', $id)->delete();
        return Redirect::to("list")->withSuccess('Great! data successfully deleted');
    }

}

Start development server

You can use the following command artisan command to start the development server:

php artisan serve

The final step, go to your browser and type the below-given URL into your browser:

http://localhost:8000/list

OR

http://localhost/project-name/public/list