Store JSON Format Data in Database using Laravel

Last updated on December 29, 2022

Storing JSON formatted data into a database can save you from a lot of coding. But sometimes it adds difficulties to extend the feature. It can be good if you want to just save and show the data.

Store JSON Format data in the Database

Before going into detail, do you have the Laravel framework installed? If not then you need to install the Laravel fresh application on the local web server.

In this article, we will be storing users’ data in a profiles table. We will need a few files in Laravel to store JSON format data. Let’s create each step by step.

Create Migration to Create Profiles Table

Open the git bash and go to your project directory, then run the below command,

php artisan make:migration create_profiles_table --create=profiles

It creates the migration file in the database/migrations directory. At the end of the above command –create=profiles means it is a new table.

public function up()
{
    Schema::create('profiles', function (Blueprint $table) {
        $table->id();
        $table->json(‘data’)->nullable();
        $table->timestamps();
    });
}

In up() method of profiles migration, we added 1 line script that will create a data column in the profiles database. Next, run the below command to create the table through profile migration.

php artisan migrate

Create Model for Profiles Table

Next, create a model for the profiles table that would help to build the query. Run the below command in the terminal or git bash,

php artisan make:model Profile

It will create Profile.php inside the models directory with Profile class that extends eloquent model class.

Create Profile Controller

The controller is the main file that handles incoming requests. In the profile controller, we will be converting incoming data into JSON and then storing it in the database. Let’s create it by running the below command,

php artisan make:controller ProfileController

This command will create a file into app/Http/Controllers directory. It contains ProfileController class that extends the Controller class. Next, create two methods, one index, and another store.

public function index()
{
    return view('create-profile');
}

index() method loads the profile.blade.php from resources/views directory.

public function store(Request $request)
{
    $requestsFromBlade = json_encode($request->all());
    $profile = new Profile();
    $profile->data = $requestsFromBlade;
    $profile->save();
    
    return back()->with('message', 'Profile data has been inserted successfully.');
}

store() method gets incoming requests from the blade file and converts them into JSON. After that, it stores in the profiles table. In the end, it redirects back to the previous route with a message. Below is the complete code of ProfileController.

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Profile;

class ProfileController extends Controller
{
    public function index()
    {
        return view('create-profile');
    }

    public function store(Request $request)
    {
        $requestsFromBlade = json_encode($request->all());
        $profile = new Profile();
        $profile->data = $requestsFromBlade;
        $profile->save();
        
        return back()->with('message', 'Profile data has been inserted successfully.');
    }
}

Create Routes

Now, let’s create a route that will provide us a URL to access the controller’s method through the browser, or in simple words, we will run the controller’s method in the browser. Add the following routes in the routes/web.php

Route::get('/create-profile', [ProfileController::class, 'index']);
Route::post('/store-profile', [ProfileController::class, 'store'])->name('store-profile');

These are two different routes, in which the first is attached with the index() method and the second is attached with the store() method.

Create Blade File

Next. create create-profile.blade.php inside the resources/views directory. Then add the following HTML form

<form method="post" action="{{route('store-profile')}}">
    @csrf
    <label>Name:</label>
    <input type="text" name="name"><br />
    <label>Email:</label>
    <input type="text" name="email"><br />
    <button type="submit" >Add Profile</button>
</form>

Now, you are all set and you can test it on the browser. To test you need to run the below command that would serve the Laravel application on the local development server

php artisan serve

And then run the following URL into the browser.

http://127.0.0.1:8000/create-profile

Conclusion

In this article, we have learned to store incoming requests from the blade file into the database after converting them into JSON.


Written by
I am a skilled full-stack developer with extensive experience in creating and deploying large and small-scale applications. My expertise spans front-end and back-end technologies, along with database management and server-side programming.

Share on:

Related Posts

Tags: Laravel,