Delete data from Mysql Database Table using Laravel 5

In this tutorial we will learn how to delete record into the database using Laravel 5. In my previous tutorial you learned how to updating data into database using modular programming in laravel 5. Laravel allow us to delete record from table two types first is soft delete and another is forceDelete with the respective functions delete()and forceDelete(). We already learned how to create database connection as follows :

'connections' => [
        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'your database name'),
            'username' => env('DB_USERNAME', 'your db username'),
            'password' => env('DB_PASSWORD', 'if you have'),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],
    ],

After configure database connection go to your database and create one table with name blog.

CREATE TABLE ` blog` (
  `blog_id` int(11) NOT NULL,
  `blog_name` varchar(255) NOT NULL,
  `blog_url` varchar(255) NOT NULL,
  `blog_desc` text NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

If table created into the database now you need to insert record in into the blog table for delete.
For delete records first you need pass primary key into your function using below route :

        Route::get('delete/{blog_id}','BlogController@deleteBlog');

Now you have to create function for delete record/data from table which is:

public function deleteBlog($blog_id) {
	$blog = Blog::find($blog_id);    
	$blog>delete();
	return Redirect::back(); // or whenever you want to redirect that page
}

There is one another way to Delete data from Mysql Database Table using Laravel 5 which is :

$blog = BlogModel::find($blog_id);    
$blog->forceDelete();

Now just do code for submit data so we will do code in model file which is in BlogModel.php.

<?php 
namespace App\Modules\Blog\Models;
use Illuminate\Database\Eloquent\Model;
use DB;
class BlogModel extends Model
{
	/**
	 * Added just to demonstrate that models work
	 * @return String
	 */
	protected $table = 'blog';
        protected $primaryKey = 'blog_id';
        protected $fillable   = ['blog_id','blog_name', 'blog_url','blog_desc'];
        protected $blog_id = 0;
        protected $blog_name = '';
        protected $blog_url = '';
        protected $blog_desc = '';
        public $timestamps = false; // for false updated_at and created_at
}

Above code you can see fillable variable for declare and assign primary key in $primaryKey variable.
Now we will do code in controller to show and update action to save data into database.

<?php
namespace App\Modules\Blog\Controllers;
use App\Http\Controllers\Controller;
use App\Modules\Blog\Models\BlogModel;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;

/**
 * BlogController
 *
 * Controller to house all the functionality directly
 * related to the Blog.
 */
class BlogController extends Controller {

    protected $blogModel;

    function __construct(BlogModel $blogModel) {
        $this->blogModel = $blogModel;
    }

    public function index(Request $request) {
        

        return view('Blog::index');
               
    }

    public function deleteBlog($blog_id) {
	$blog = Blog::find($blog_id);    
	$blog->delete();
	return Redirect::back(); // or whenever you want to redirect that page
}
}

For all that above functionality you have to set delete call from routes are as follows:

<?php 
/*
|--------------------------------------------------------------------------
| Blog Module Routes
|--------------------------------------------------------------------------
|
| All the routes related to the Blog module have to go in here. Make sure
| to change the namespace in case you decide to change the 
| namespace/structure of controllers.
|
*/
Route::group(['prefix' => 'blog', 'namespace' => 'App\Modules\Blog\Controllers'], function () {
	Route::get('/', ['as' => 'blog.index', 'uses' => 'BlogController@index']);
              Route::get('delete/{blog_id}','BlogController@deleteBlog');
});

Above route code will open and redirect immediately to your module

<your project url>/public/blog/delete/{blog id which you will pass to delete}