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
In this step, we'll install the laravel 11 application using the following command.
composer create-project laravel/laravel laravel-11-example
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');
}
};
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),
);
}
}
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']);
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);
}
}
Now, run the laravel 11 application using the following command.
php artisan serve
You might also like:
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...
Aug-19-2022
Hey everyone! If you're a developer working with Angular, you know how exciting it is when a new version is released...
Mar-18-2024
In this article, we will show you laravel 9 user role and permission with an example. here we will see how to set u...
Mar-02-2022
Today, we will see laravel 8 google bar chart example, Google charts is use to visualize data on you...
Jul-23-2021