Update data into Mysql Database Table using Laravel 5

In this tutorial we will learn how to update data into Mysql database Table using Laravel 5 . In my previous tutorial you learned how to insertion of data into database using modular programming in laravel 5. 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,
        ],
    ],
[/pph]
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 update.
For update records first you need to fetch row from database for update so now you need to code into routes.php file which is available in Blog/routes.php are as follows:

        Route::get('edit_blog/{blog_id}', ['as' => 'blog.edit_blog', 'uses' => 'BlogController@editBlog']);

Now you have to create function for get data from table which is:

public function editBlog($blog_id) {
        $blog = BlogModel::find($blog_id);
        return view('Blog::edit_blog', ['blog' => $blog]);
    }

For showing fetched row you need to create edit_blog.blade.php file and do following code in that:

<form method="POST" action="{{url('blog/update')}}"  enctype="multipart/form-data" class="form-horizontal">
<div  class="col-sm-12">
<div id="blogArea">
	<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" value="{{ $blog->blog_name}}"/>
			<input class="form-control" type="hidden" name="blog_id" id="blog_id" value="{{ $blog->blog_id}}"/>
		</div>
	</div>
	<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" value="{{ $blog->blog_url}}"/>
		</div>
	</div>
	<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">{{ $blog->blog_desc}}</textarea>
		</div>
	</div>
	<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>
	<div class="clearfix visible-lg"></div>
</div>
</div>
</form>

Above form show your existing data now you need to update in update action which is available in BlogController but before that you have to set route again like:

Route::post('update','BlogController@update');

And now create method for update in BlogController :

public function update(Request $request) {
        $blog_obj = new BlogModel();
        $blog_obj->blog_id = Request::input('blog_id');
        $blog = BlogModel::find($blog_obj->blog_id); // Eloquent Model
        $blog->update(Input::only('blog_name', 'blog_url','blog_desc'));
        return redirect('/blog');
    }

In update method we set all input data for table column and redirect to blog/index.
Now you will see complete code for edit_blog,BlogModel,BlogController And Route for update records.
Inside Blog/views folder create edit_blog.blade.php file with following code with action update and post method.

edit_blog.blade.php
	<form method="POST" action="{{url('blog/update')}}"  enctype="multipart/form-data" class="form-horizontal">
<div  class="col-sm-12">
<div id="blogArea">
	<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" value="{{ $blog->blog_name}}"/>
			<input class="form-control" type="hidden" name="blog_id" id="blog_id" value="{{ $blog->blog_id}}"/>
		</div>
	</div>
	<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" value="{{ $blog->blog_url}}"/>
		</div>
	</div>
	<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">{{ $blog->blog_desc}}</textarea>
		</div>
	</div>
	<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>
	<div class="clearfix visible-lg"></div>
</div>
</div>
</form>

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 editBlog($blog_id) {
        $blog = BlogModel::find($blog_id);
        return view('Blog::edit_blog', ['blog' => $blog]);
    }

    public function update(Request $request) {
        $blog_obj = new BlogModel();
        $blog_obj->blog_id = Request::input('blog_id');
        $blog = BlogModel::find($blog_obj->blog_id); // Eloquent Model
        $blog->update(Input::only('blog_name', 'blog_url','blog_desc', 'blog_cat_id', 'blog_is_popular','blog_meta_title','blog_meta_keywords','blog_meta_description', 'blog_status'));
        return redirect('/blog');
    }

}

For all that above functionality you have to set edit_blog into the 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('edit_blog/{pub_id}', ['as' => 'blog.edit_blog', 'uses' => 'BlogController@editBlog']);
        Route::post('update','BlogController@update');        
});

Above route code will open your module on

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