In this tutorial you will learn about the Laravel 9 Store JSON Format Data to Database and its application with practical example.
In this Laravel 9 Store JSON Format Data to Database tutorial I will show you how to store/insert json form data into mysql database. I will also show you how to convert and insert json format data into MySQL database. In this tutorial you will learn convert form data into json format and to save data from JSON data into MySQL database.
Laravel 9 Store JSON Format Data to Database
In this step by step tutorial you will learn how to store JSON format data into MySQL database in laravel 9 application. Please follow the instruction given below:
- Install Laravel 9
- Setup Database
- Generate migration and model
- Migrate Database
- Add Route
- Create controller
- Create blade view
- Start Development Server
Install Laravel 9
First of all we need to create a fresh laravel project, download and install Laravel 9 using the below command
1 |
composer create-project --prefer-dist laravel/laravel LaravelJson |
Setup Database
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=here your database name here DB_USERNAME=here database username here DB_PASSWORD=here database password here |
Migrate Database
Run the migration to create database table using following artisan command:
1 |
php artisan migrate |
Generate migration and model
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan make:model Test -m |
Once above command is executed there will be a migration file created inside database/migrations/ directory, just open create_tests_table.php migration file and update the function up() method as following:
1 2 3 4 5 6 7 8 9 |
public function up() { Schema::create('tests', function (Blueprint $table) { $table->increments('id'); $table->string('token')->nullable(); $table->text('data')->nullable(); $table->timestamps(); }); } |
Now, run the migration to create database table using following artisan command:
1 |
php artisan migrate |
Add Route
In this step, we will create two routes in the web.php file. One to display form and the second route is to store data in JSON to MySQL database. Open app/routes/web.php file and put the following routes in it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\JsonController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('laravel-json', [JsonController::class, 'index']); Route::post('store-json', [JsonController::class, 'store']); |
Create controller
Now, lets create a controller named JsonController using command given below –
1 |
php artisan make:controller JsonController |
Once the above command executed, it will create a controller file JsonController.php in app/Http/Controllers/ directory. Open the JsonController.php file and put the following code in it.
app/Http/Controllers/JsonController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php namespace App\Http\Controllers; use Redirect,Response; use Illuminate\Http\Request; use App\Models\Test; 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); Test::insert($test); return Redirect::to("laravel-json")->withSuccess('Great! Successfully store data in json format in datbase'); } } |
Create blade view
In this step, we will create a blade view name json.blade.php. Go to resources/views and create one blade view file.
json.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<!doctype html> <html lang="en"> <head> <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="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" /> <style> .error{ color:red; } </style> </head> <body> <div class="container"> <h2 style="margin-top: 10px;">Laravel Store Data To Json Format In Database</h2> <br> <br> @if ($message = Session::get('success')) <div class="alert alert-success alert-block"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>{{ $message }}</strong> </div> <br> @endif <form id="laravel_json" method="post" action="{{url('store-json')}}"> @csrf <div class="form-group"> <label for="formGroupExampleInput">Name</label> <input type="text" name="name" class="form-control" id="formGroupExampleInput" placeholder="Please enter name"> </div> <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> <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> <div class="form-group"> <button type="submit" class="btn btn-success">Submit</button> </div> </form> </div> </body> </html> |
Start Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/laravel-json |