Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Inquisitive's avatar

Store an array of JSON data in mysql database

 $arr = [
            'organization_id' => $organization_id,
            'user_id' => $user_id
        ];
        $data = json_encode($arr);

With above code I am able to insert data as

{"organization_id":1,"user_id":1}

But, next time I run the same function (with different organization and user id value), I want to store this data as follow:

[{"organization_id":1,"user_id":1}, {"organization_id":2,"user_id":5}]

How can I achieve this.

0 likes
1 reply
Braunson's avatar

Is your table your storing the json data set to json? You should be able to pass it a normal PHP array and it should encode and store it/return it for you accordingly. Make sure to set it as an array in your Model's casts array.

Learn more about it https://mattstauffer.com/blog/new-json-column-where-and-update-syntax-in-laravel-5-3/

Migrations support json and jsonb columns (https://laravel.com/docs/5.6/migrations#creating-columns)

Docs + Articles:

Please or to participate in this conversation.