Laravel 9 Store JSON Format Data to Database

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

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.

Migrate Database

Run the migration to create database table using following artisan command:

Generate migration and model

Now, in this step we will create model and migration file. Please run the following command:

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:

Now, run the migration to create database table using following artisan command:

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:

Create controller

Now, lets create a controller named JsonController using command given below –

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

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

Start Development Server

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

In this tutorial we have learn about the Laravel 9 Store JSON Format Data to Database and its application with practical example. I hope you will like this tutorial.