Laravel Working With Json Table Column Example

How to Store multiple values in single field laravel is the todays topic. Sometimes we need to store multiple key and value in single column in Laravel. But do you know how we can do that? we can use pivot table to solve this issue. But here i am going to use json column to store multiple records with key and value for respect to key.

From this laravel json tutorial you will also learn how to insert json data into mysql using laravel. In this tutorial we will insert multiple properties for a single product like size, price, value, color etc from a single method into a single field with json.

So let's se how we can store json data into database. So let's start laravel json column example tutorial.

Preview : Store json data form

store-json-data-laravel

 

Preview : After fetching json data

laravel-working-with-json

 

Step 1 : Create Model

In this we need product model. So let's create it to store json data.

php artisan make:model Product -m

 

Now open product model and update like below.

app/Product.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $guarded = [];

    protected $casts = [
        'properties' => 'array'
    ];

    public function setPropertiesAttribute($value)
	{
	    $properties = [];

	    foreach ($value as $array_item) {
	        if (!is_null($array_item['key'])) {
	            $properties[] = $array_item;
	        }
	    }

	    $this->attributes['properties'] = json_encode($properties);
	}

}

 

and open migration file and update it like below.

database/migration/create_products_table.php

  public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->decimal('price', 15, 2);
            $table->json('properties');
            $table->timestamps();
        });
    }

 

Step 2 : Create Route

We need many route for storing json data into json column.

routes/web.php

Route::get('produc/create','[email protected]_product_form')->name('produc.create');
Route::post('produc/create','[email protected]');
Route::get('produc','[email protected]')->name('produc.index');

 

Step 3 : Create Controller 

In this step we need to create product controller. So create it and update this controller like below

app/Http/Controllers/ProductController.php

namespace App\Http\Controllers;

use App\Category;
use App\Http\Controllers\Controller;
use App\Product;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    public function show_product_form()
    {
    	return view('create');
    }

    public function store(Request $request)
    {
    	$product = Product::create($request->all());
        
        return redirect()->back();

    }

    public function index()
    {   
    	$post = Product::all();

    	return view('index',['products' => $post]);
    }
}

 

Step 4 : Create Blade File

Now we are in the final step and all are set to go. So how to insert json data into mysql using laravel we will know. Now create below file and paste this code in your file.

resources/views/create.blade.php

 

resources/views/index.blade.php

 

Recommended : Avoid Pivot Table and Use Json Column in Laravel

 

Hope this Laravel json tutorial will help you.