Laravel Json – Store Json Data to Mysql Database Example

Laravel Json – Store Json Data to Mysql Database Example

Sometime, In laravel projects we need to store data in database into json format. In this simple laravel 6 store json data example, we will store form data to json format in database step by step from scratch.

Just follow few steps and simply store your data to database into json format laravel based project.

Laravel store/save json data into mysql database

  • Install Laravel Latest Setup
  • Setup Database
  • Generate migration and model
  • Migrate Database
  • Add Route
  • Create controller
  • Create blade view
  • Start Development Server
  • Conclusion

Install Laravel Latest Setup

First of we need to download laravel fresh setup name Laravel Json. Use the below command and download fresh new laravel setup :

composer create-project --prefer-dist laravel/laravel LaravelJson

Setup Database

After successfully download laravel Application, Go to your project .env file and set up database credential and move next step :

DB_DATABASE=here your database name here
DB_USERNAME=here database username here
DB_PASSWORD=here database password here

Migrate Database

Before we run php artisan migrate command go to app/providers/AppServiceProvider.php and put the below code :

use Illuminate\Support\Facades\Schema;

function boot()

Generate migration and model

In this step, we will generate one migration file with create one model name Test using the below command :

php artisan make:model Test -m

After creating the model and migration file. Go to app/database/migration and find the migration file name create_tests_table.php and replace up() the below code here :

   public function up()
        Schema::create('tests', function (Blueprint $table) {

Now we will run the below command. It will create some tables in our database, Use the below command :

php artisan migrate

Add Route

We will create two routes in web.php file for one is display form and second route is store data in json to mysql database . Go to app/routes/web.php file and create below routes here :

 Route::get('/laravel-json', 'JsonController@index');
Route::post('/store-json', 'JsonController@store');

Create Controller

In this step we will create one controller name JsonController. Use the below command and create controller :

php artisan make:controller JsonController 

After successfully create controller go to app/http/controllers open JsonController and create two method here. First method display the form and second method convert laravel string form data to json format and save it to database :


namespace App\Http\Controllers;
use App\Test;
use Redirect,Response;
use Illuminate\Http\Request;

class JsonController extends Controller
   public function index()
      return view('json_form');

  public function store(Request $request)
      $data = $request->only('name','email','mobile_number');
      $test['token'] = time();
      $test['data'] = json_encode($data);
      return Redirect::to("laravel-json")->withSuccess('Great! Successfully store data in json format in datbase');


Create Blade view

Now we will create one blade view name json.blade.php let go to resources/views and create one blade view file.

After create blade view file put the below code here :

<!doctype html>
<html lang="en">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="csrf-token" content="{{ csrf_token() }}">
  <title>Laravel Store Data To Json Format In Database -</title>
  <link rel="stylesheet" href="" />
   .error{ color:red; } 


<div class="container">
    <h2 style="margin-top: 10px;">Laravel Store Data To Json Format In Database -</h2>

    @if ($message = Session::get('success'))
    <div class="alert alert-success alert-block">
        <button type="button" class="close" data-dismiss="alert">×</button>
          <strong>{{ $message }}</strong>
    <form id="laravel_json" method="post" action="{{url('store-json')}}">
      <div class="form-group">
        <label for="formGroupExampleInput">Name</label>
        <input type="text" name="name" class="form-control" id="formGroupExampleInput" placeholder="Please enter name">
      <div class="form-group">
        <label for="email">Email Id</label>
        <input type="text" name="email" class="form-control" id="email" placeholder="Please enter email id">
      <div class="form-group">
        <label for="mobile_number">Mobile Number</label>
        <input type="text" name="mobile_number" class="form-control" id="mobile_number" placeholder="Please enter mobile number">
      <div class="form-group">
       <button type="submit" class="btn btn-success">Submit</button>



Start Development Server

We need to start development server. Use the php artisan serve command and start your server :

 php artisan serve
If you want to run the project diffrent port so use this below command
php artisan serve --port=8080

Now we are ready to run our laravel json data stored to datbase example so run below command to quick run.

Or direct hit in your browser


In this laravel tutorial for store json data in database, We have successfully laravel project stored data in json format in our database. our examples run quickly.

Laravel Store Json example look like this :

If you have any questions or thoughts to share, use the comment form below to reach us.

AuthorTuts Make

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Codeigniter, Vue JS, Angular JS, React Js, WordPress and Bootstrap from a starting stage.

One reply to Laravel Json – Store Json Data to Mysql Database Example

Leave a Reply

Your email address will not be published. Required fields are marked *