Laravel- how to update JSON data type column in MySQL 5.7

Laravel- how to update JSON data type column in MySQL 5.7

In this post we will give you information about Laravel- how to update JSON data type column in MySQL 5.7. Hear we will give you detail about Laravel- how to update JSON data type column in MySQL 5.7And how to use it also give you demo for it if it is necessary.

In this tutorial, I will tell you how to update json data for a specific key exist in json object in MySQL 5.7

Laravel provides the way to easily modify the value of specific key in JSON column.

As per Normalization technique, every column should have a single value, but in some scenario, there is need to hold the json data.


Note : Do not save json data in a column which is regularly used for searching because JSON fields can not be indexed.

Let’s have a table with JSON field :

Create a table for user details which hold address in json format.

CREATE TABLE 'members' (
  'id' int(11) unsigned NOT NULL AUTO_INCREMENT,
  'name' varchar(200) NOT NULL,
  'address' json DEFAULT NULL,
  PRIMARY KEY ('id')
) ENGINE=InnoDB;

In address field data will be stored in json object :

{
    "id": 1,
    "name": "Ajay",
    "address": {
        "city": "Noida",
        "country": "India"
    }
}

Ok, Let’s update the city in address field using Laravel DB query builder :

  1. DB::table('members')
  2. ->where('id',1)
  3. ->update(['address->city'=>'Varanasi']);
DB::table('members')
    ->where('id', 1)
    ->update(['address->city' => 'Varanasi']);

Now you can get the list of members from Varanasi city :

  1. $members= DB::table('members')
  2.          ->where('address->city','Varanasi')
  3.          ->get();
$members = DB::table('members')
    		 ->where('address->city', 'Varanasi')
    		 ->get();

Click here to upgrade the MySQL version from 5.5 to 5.7

Try this when you will have to work on JSON type value field.

Hope this code and post will helped you for implement Laravel- how to update JSON data type column in MySQL 5.7. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

5  +  1  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US