How to handle JSON Data in Laravel with Eloquent and JSON Columns: Complete guide 2023

Chimeremze Prevail Ejimadu
3 min readSep 12, 2023

--

Photo by vackground.com on Unsplash

In modern web applications, data often comes in the form of JSON (JavaScript Object Notation). JSON is a versatile data format that’s easy to work with, and Laravel provides excellent support for handling JSON data. In this article, let’s explore how to work with JSON data in Laravel, focusing on JSON columns in your database and using Laravel’s powerful Eloquent ORM to interact with this data.

JSON Columns in Laravel

What Are JSON Columns?

JSON columns in a database allow you to store structured data in JSON format. Laravel supports JSON columns in various database systems, including MySQL, PostgreSQL, and SQLite. These columns are incredibly flexible because they can store different types of data, such as strings, numbers, arrays, or even nested JSON objects.

Adding JSON Columns

To add a JSON column to a database table, you can use Laravel’s migration system. Here’s an example of creating a “metadata” JSON column in a “posts” table:

public function up()
{
Schema::table('posts', function (Blueprint $table) {
$table->json('metadata')->nullable();
});
}

In this example, we’ve added a nullable JSON column called “metadata” to the “posts” table.

Storing JSON Data

Inserting JSON Data

Once you have a JSON column, you can insert JSON data into it. Laravel makes it easy to work with JSON data using Eloquent models. Here’s how you can insert JSON data into the “metadata” column:

$post = new Post;
$post->title = 'Sample Post Title';
$post->metadata = ['author' => 'Prevail E.', 'tags' => ['laravel', 'json']];
$post->save();

In this example, we’ve assigned an associative array to the “metadata” attribute of a Post model and saved it to the database.

Updating JSON Data

You can also update JSON data in a JSON column. Let’s say you want to update the “tags” in the “metadata” column:

$post = Post::find(1);
$metadata = $post->metadata;
$metadata['tags'][] = 'eloquent';
$post->metadata = $metadata;
$post->save();

In this example, we retrieved the existing JSON data, modified it, and then saved it back to the database.

Retrieving JSON Data

Querying JSON Data

Laravel’s Eloquent ORM allows you to query JSON data easily. For instance, if you want to find all posts where the “metadata” JSON column contains the “laravel” tag, you can use the whereJsonContains method:

$laravelPosts = Post::whereJsonContains('metadata->tags', 'laravel')->get();

This query retrieves all posts where the “tags” array in the “metadata” column contains the string “laravel.”

Accessing JSON Attributes

You can access JSON attributes as if they were regular object properties. For example, to retrieve the “author” from the “metadata” JSON column:

$post = Post::find(1);
$author = $post->metadata['author'];

Laravel’s Eloquent takes care of converting the JSON data into a PHP array or object for you.

Modifying JSON Data

Updating JSON Attributes

Updating JSON attributes is straightforward. Suppose you want to change the author’s name in the “metadata” JSON column:

$post = Post::find(1);
$post->metadata['author'] = 'Edited Prevail E.';
$post->save();

Deleting JSON Attributes

You can also delete JSON attributes using the forget method. To remove the "tags" attribute from the "metadata" JSON column:

$post = Post::find(1);
unset($post->metadata['tags']);
$post->save();

Conclusion

Working with JSON data in Laravel, whether it’s storing, retrieving, updating, or deleting, is made effortless thanks to Laravel’s support for JSON columns and the power of Eloquent. JSON columns provide flexibility and versatility, allowing you to work with structured data in a way that fits your application’s needs. With these, you can handle JSON data seamlessly in your Laravel projects, making your web applications even more robust and dynamic.

Stay tuned!!! I will be back with some more cool Laravel tutorials in the next article. I hope you liked the article. Don’t forget to follow me 😇 and give some clap 👏. And if you have any questions feel free to comment.

Thank you.

Thanks a lot for reading till end. Follow or contact me via:
Twitter: https://twitter.com/EjimaduPrevail
Email: prevailexcellent@gmail.com
Github: https://github.com/PrevailExcel
LinkedIn: https://www.linkedin.com/in/chimeremeze-prevail-ejimadu-3a3535219
BuyMeCoffee: https://www.buymeacoffee.com/prevail
Chimeremeze Prevail Ejimadu

--

--

Chimeremze Prevail Ejimadu

Laravel Developer + Writer + Entrepreneur + Open source contributor + Founder + Open for projects & collaborations. Hit FOLLOW ⤵