Sling Academy
Home/PHP/Working with JSON columns in Eloquent: A Practical Guide

Working with JSON columns in Eloquent: A Practical Guide

Last updated: January 16, 2024

Introduction

JSON data types are a flexible means of storing structured data. When working with databases in Laravel applications, Eloquent ORM (Object-Relational Mapper) provides an elegant way to interact with JSON columns. In this comprehensive guide, we’ll walk you through the ins and outs of manipulating JSON columns using Eloquent in your Laravel projects.

Prerequisites

  • Basic knowledge of Laravel and Eloquent
  • Understanding of JSON data format
  • Laravel application with a database that supports JSON columns (MySQL, PostgreSQL, etc.)

Basic Operations on JSON Columns

Assuming you have a JSON column in your database, let’s explore some basic operations you can perform on it using Eloquent. First, define an Eloquent model for your table.

class User extends Model
{
    // ... other model properties and methods
}

Let’s say ‘profile’ is a JSON column in your ‘users’ table. You can directly access JSON fields using the ‘->’ arrow syntax.

$user = User::find(1);
echo $user->profile->age; // Accessing JSON object field

When you’re saving data, you can use the same syntax:

$user = User::find(1);
$user->profile->age = 30;
$user->save();

Querying JSON Columns

Querying JSON columns involves using specific methods that understand the JSON structure. Here’s how you select users by field in the JSON column.

$users = User::where('profile->language', 'en')->get();

You can also search for items with specific properties inside a JSON array:

$users = User::whereJsonContains('profile->tags', ['author'])->get();

Advance Querying Techniques

What if we need to perform more complex queries, such as ordering by a JSON field? Eloquent supports these operations as well.

$users = User::orderBy('profile->reputation')->get();

For retrieving only part of the JSON, Laravel provides a ‘selectRaw’ method:

$users = User::selectRaw('profile->>$.settings as user_settings')->get();

To update parts of the JSON column without touching other properties, use ‘update’ with a JSON path:

User::where('id', 1)->update(['profile->age' => 31]);

Working with JSON Collections

Laravel even casts JSON columns to ‘Illuminate\Support\Collection’, allowing you to leverage all Collection methods on your JSON data.

$user = User::find(1);
$interests = collect($user->profile->interests);
$sortedInterests = $interests->sort();

Validation of JSON Inputs

To ensure your application maintains valid JSON structures, use Laravel’s built-in validation rules for JSON.

use Illuminate\Support\Facades\Validator;

$data = [
    'profile' => '{"age":30, "language":"en"}'
];

$validator = Validator::make($data, [
    'profile' => 'required|json'
]);

if ($validator->fails()) {
    return $validator->errors();
}
// Proceed with data manipulation knowing the JSON is valid.

Storytelling Data with JSON Relationships

JSON columns might also represent relationships. You might have a scenario where JSON columns store keys that are connected to another model:

class User extends Model
{
    public function getOrdersAttribute()
    {
        return Order::whereIn('id', $this->profile->orders)->get();
    }
}

Inherently, Eloquent does not directly join JSON fields to relational data, but with accessor methods, you can articulate an effective relationship.

Conclusion

JSON with Eloquent is a clash of structure and flexibility, opening vast possibilities for developers. Leveraging JSON columns in Eloquent adds a powerful layer of data manipulation and querying in your Laravel application. This guide walked through several key techniques, from basic operations to advanced queries that let you get the most out of this functionality.

Next Article: Eloquent: Delete multiple records with an array of IDs

Previous Article: Eloquent: How to Check if a Model Attribute Changed

Series: Laravel & Eloquent Tutorials

PHP

You May Also Like

  • Pandas DataFrame.value_counts() method: Explained with examples
  • Constructor Property Promotion in PHP: Tutorial & Examples
  • Understanding mixed types in PHP (5 examples)
  • Union Types in PHP: A practical guide (5 examples)
  • PHP: How to implement type checking in a function (PHP 8+)
  • Symfony + Doctrine: Implementing cursor-based pagination
  • Laravel + Eloquent: How to Group Data by Multiple Columns
  • PHP: How to convert CSV data to HTML tables
  • Using ‘never’ return type in PHP (PHP 8.1+)
  • Nullable (Optional) Types in PHP: A practical guide (5 examples)
  • Explore Attributes (Annotations) in Modern PHP (5 examples)
  • An introduction to WeakMap in PHP (6 examples)
  • Type Declarations for Class Properties in PHP (5 examples)
  • Static Return Type in PHP: Explained with examples
  • PHP: Using DocBlock comments to annotate variables
  • PHP: How to ping a server/website and get the response time
  • PHP: 3 Ways to Get City/Country from IP Address
  • PHP: How to find the mode(s) of an array (4 examples)
  • PHP: Calculate standard deviation & variance of an array