Insert data into Mysql Database Table using Laravel 5

In this tutorial we will learn insertion of record into the database using Laravel 5. In my previous tutorial you learn how to develop modules in Laravel5. Towards we will create connection with mysql database into the database file inside config folder database.php [config/database.php] over there we need to set following configuration which are 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,

Now we are ready to code Insert data into Mysql database Table using Laravel 5.
Now you need to create form for insertion of record into the blog table. Inside views folder create add_blog.blade.php file with following code with action post method.

	<form method="POST" action="{{url('blog/store')}}"  enctype="multipart/form-data" class="form-horizontal">
                        <div  class="col-sm-12">
                            <div id="reportArea">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label"> Blog Name</label>
                                    <div class="col-sm-6">
                                        <input class="form-control" type="text" name="blog_name" id="blog_name" />
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">Blog Url</label>
                                    <div class="col-sm-6">
                                        <input class="form-control" type="text" name="blog_url" id="blog_url" />
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">Blog Description</label>
                                    <div class="col-sm-6">
                                        <textarea class="ckeditor" name="blog_desc" id="blog_desc"></textarea>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label"> </label>
                                    <input class="btn btn-primary col-sm-3" type="submit" name="submit" id="submit" value="Submit"/>
                                <div class="clearfix visible-lg"></div>

Now just do code for submit data so wee will do code in model file which is BlogModel.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 add blog form and store action to save data into database. In the controller you have to create addBlog function to show form and create store method to save data in table and then redirect from store function to where you want.

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;

 * IndexController
 * 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 addBlog() {
        return view('Blog::add_blog');

    public function store(Request $request) {
        $blog = new BlogModel;
        return redirect('/blog');


For all that above functionality you have to declare add_blog into the routes are as follows:

| 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('add_blog', ['as' => 'blog.add_blog', 'uses' => 'BlogController@addBlog']);

Above route code will open your module on

<your project url>/public/blog/add_blog
  • Tansukh Parmar

    Hello do not have the model work in this code