SyntaxStudy
Sign Up
MySQL Working with JSON Columns in Laravel
MySQL Intermediate 4 min read

Working with JSON Columns in Laravel

JSON in Laravel

Laravel Eloquent supports JSON column querying with whereJsonContains() and arrow notation in migrations.

Example
// Migration
$table->json('attributes');

// Eloquent queries
Product::whereJsonContains('attributes->tags', 'php')->get();
Product::where('attributes->ram', 16)->get();

// Updating JSON fields
$product->forceFill(['attributes->ram' => 32])->save();
Pro Tip

Cast JSON columns as arrays in your model: protected $casts = ["attributes" => "array"].