D
D
Dmitry Sudarkin2018-07-03 12:23:32
JSON
Dmitry Sudarkin, 2018-07-03 12:23:32

How to store json data in database?

And again, hello everyone! )
I'm not saving json data correctly. I don't know how to do it right... Tell me who knows.
I have an elements field with json data . It is required to update the parameter . I update ...
{"param": {"0": "0"}}

Section::where([
    "id" => 1
])->update([
    "elements->param" => json_encode(["TEST" => "TEST"])
]);

But I get this How to save correctly so that json saves the correct structure, and it turned out like this
{"param": "{\"TEST\":\"TEST\"}"}
{"param": {"TEST": "TEST"}}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mShpakov, 2018-07-03
@GrozInventor

No need to do json_encode
Write casts object/array for the desired column in the model and save like this:

Section::where([
    "id" => 1
])->update([
    "elements->param" => ["TEST" => "TEST"]
]);

And eloquent itself will return the data already parsed to you

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question