bhatt
bhatt

Reputation: 133

How to store array data into database in Laravel

I have some JSON data; I decode that JSON data into an array, now I need to save the link column, id column and username column into my database table (the table name may be the product). So how to insert/save this column into the database in Laravel? My single node array structure format is below.

   {
        0 => {
          +"attribution": null
          +"tags": array:11 
          +"type": "image"
          +"location": null
          +"filter": "Normal"
          +"created_time": ""
          +"link": "https://www.url.com/p/myimage1/"
          +"id": "4354"
          +"user": {
            +"username": "user"
            +"profile_picture": "https://mypic.jpg"
            +"id": "224"
            +"full_name": ""
          }
        }
       
        }

Upvotes: 1

Views: 5486

Answers (2)

Naveed Ali
Naveed Ali

Reputation: 1045

you can use toJson() method for it as below:

$table->toJson('options');

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163788

One way to do that is to save original json data or convert array to json with ->toJson() or json_encode() and save it to a DB as Json data:

$table->json('options'); // JSON equivalent for the database.

https://laravel.com/docs/5.2/migrations#writing-migrations

Upvotes: 2

Related Questions