How to Store JSON Data in Database in Laravel 11

Websolutionstuff | Jul-19-2024 | Categories : Laravel MySQL

Hello, laravel web developers! In this article, we'll see how to store JSON data in the database in laravel 11. JSON is an open standard file format and data interchange format that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and arrays.

JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. JSON is popular with developers because it's a flexible format for data exchange that enjoys wide support in modern programming languages and software systems.

Laravel 11 Store JSON Data in MySQL Database

laravel 11 store json data in database

 

Step 1: Install Laravel 11 Application

In this step, we'll install the laravel 11 application using the following command.

composer create-project laravel/laravel laravel-11-example

 

Step 2: Create Migration

Next, we'll create migration and store JSON data in the database.

php artisan make:migration create_products_table

Migration:

<?php
  
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
  
return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up(): void
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->json('details')->nullable();
            $table->timestamps();
        });
    }
  
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down(): void
    {
        Schema::dropIfExists('products');
    }
};

 

Step 3: Create Model

Then, we'll create a model Product.php with getter and setter.

php artisan make:model Product

app/Models/Product.php

<?php
  
namespace App\Models;
  
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
  
class Product extends Model
{
    use HasFactory;
  
    /**
     * Write code on Method
     *
     * @return response()
     */
    protected $fillable = [
        'title', 'details' 
    ]; 
  
    /**
     * Get the user's first name.
     *
     * @return \Illuminate\Database\Eloquent\Casts\Attribute
     */
    protected function details(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => json_decode($value, true),
            set: fn ($value) => json_encode($value),
        );
    } 
}
 
Step 4: Define Route

Next, we'll define the route in the web.php file.

routes/web.php

<?php
 
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;
     
Route::get('products', [ProductController::class, 'index']);
 
Step 5: Create Controller

Then, we'll create a controller and add the index() function.

app/Http/Controllers/ProductController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\Product;
  
class ProductController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $input = [
            'name' => 'Laptop',
            'details' => [
                'brand' => 'HP',
                'price' => '5000',
                'ram' => '16 GB',
                'ssd' => '512 GB'
            ]
        ];
  
        $product = Product::create($input);
  
        dd($product->details);
  
    }
}
 
Step 6: Run the Laravel 11 Application

Now, run the laravel 11 application using the following command.

php artisan serve

 


You might also like:

Recommended Post
Featured Post
How To Bind Data In React JS
How To Bind Data In React JS

In this article, we will see how to bind data in React JS. Also, we will see how to bind the variable value in the...

Read More

Aug-19-2022

How to Upgrade from Angular 16 to Angular 17
How to Upgrade from Angular 16...

Hey everyone! If you're a developer working with Angular, you know how exciting it is when a new version is released...

Read More

Mar-18-2024

Laravel 9 User Role and Permission
Laravel 9 User Role and Permis...

In this article, we will show you laravel 9 user role and permission with an example. here we will see how to set u...

Read More

Mar-02-2022

Laravel 8 Google Bar Chart Example
Laravel 8 Google Bar Chart Exa...

Today, we will see laravel 8 google bar chart example, Google charts is use to visualize data on you...

Read More

Jul-23-2021