D
D
Dadoshik2017-07-05 16:03:40
JSON
Dadoshik, 2017-07-05 16:03:40

How to add data to the existing ones in a JSON format cell?

Laravel 5.4
MySQL 5.7
The task was to save NickName WE of users in the database who decided to participate in the draw.
It is most convenient to store them in an array or in a cell in JSON format.
Problem:
If there is already 1 nickname in a cell, how can I add a second one to it?
I did this:
I got the old value and assigned the new one, but the non-names began to merge, I had to separate them with '|' and when fetching using explode(), remove the separator and get an array of names.

$slash = '|';
            $test->party = $test->party.= $slash .= $lottery->nickname; 
            $test->sum = $test->sum + 1;
            $test->save();

Can anyone suggest a more elegant way? How to add a new nickname to an existing one, and then make a selection.
In programming 1.5 months. I couldn't think of a better crutch :(

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Abdula Magomedov, 2017-07-05
@Dadoshik

and here explode if you use json.
json_encode() -> json_decode()
serialize() -> unserialize()

A
Alexander Aksentiev, 2017-07-05
@Sanasol

It is most convenient to store them in an array or in a cell in JSON format.

No, it's best to do it right.
hasMany is called. Relations between Models and all that.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question