Handling JSON Data In Laravel with Eloquent And JSON Columns: Complete Guide

Ogbodo Nelson
3 min readJun 9, 2024

--

Data can often be displayed as JSON (JavaScript Object Notation) in contemporary web applications. Laravel offers great support for managing JSON data, which is an easy-to-work-with and versatile data format. In this post, we’ll look at how to work with JSON data in Laravel, specifically concentrating on JSON columns in your database and utilizing the Eloquent Model in Laravel to manipulate this data.

What Is JSON ?

JavaScript Object Notation (JSON) could be said to be a file format, a text format, or a light-weight format for storing and transporting data.

Major Points To Take From The Definition

  1. JSON is an acronym for JavaScript Object Notation
  2. JSON is a file or text format that is used to store and transport data

Example Of JSON

{"name":"Lino", "gender":"male", "age":20}

In the example above, we have a json string containing three keys, which are (name, gender, and age), and also values assigned to each of the keys, respectively, which are all embedded within curly braces or brackets. In programming, the keys and the values are simply called key-value pairs.

What Do I Mean By JSON Columns In Laravel ?

Laravel, which is a PHP framework, makes use of an MVC (model, view, and controller) pattern to operate, but when we talk of manipulating data in the database, such as create, send, update, and delete, it’s all done using the model in Laravel. When you have JSON data and you want to store it in the database, you’ll go ahead and create a JSON column in the database that accepts and holds data of only JSON type and format, which is then considered to be a JSON column after you’ve created it.

How To Add JSON Column To The Database

As i did say earlier as regards to manipulating data in the database, to add JSON column to your database, you’ll make use of Laravel's migration system. Below is a good example of how to go about this in Laravel.

Firstly, you’ll have to create a migration file to handle all of your migrations, which will be located inside the migrations folder in your project. Let’s say you have a database table, namely students, where you store every bit of information about a student. Now, if you want to add or index information about a particular student into the database in the form of a JSON format, just go ahead and create a migration file for this and make sure the file points to that student’s table like this below.

php artisan make:migration create_students_table

After this migration file has been created, you can then create a migration code which creates a JSON column that accepts JSON data. Here’s a quick source code of what I'll look like.

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

In the example above, a public function named Up was created for me as I created a migration file to accept JSON data from each student. Inside this function, there’s a schema of tables that takes in two parameters, the first being the name of the table that you want to add in the JSON column, and ours being students; now we’ve pointed directly to our table, the second parameter being a function that then creates a JSON column for us, namely studentdata. Once you have your migration setup like this, be sure to refresh your migration for it to add up and show as a JSON column in your students table using the command below.

php artisan migrate:refresh

After you have refreshed your migration, go to your database table and check to see if the migration changes have reflected in your table. With this being said, please do well to visit my website link below for more knowledge and information, and also get in touch with me through the social links I’ll be dropping below. Thank you.

Website Link: https://techtheworld.net/
Email: ogbodo21nelson@gmail.com
Github: https://github.com/ogbodonelson
Facebook: https://web.facebook.com/profile.php?id=100093544205050

--

--