The json column in the database has a data like this:
{"data": {"total":15,"time":25}}
This is the data I want to update within it:
{"total":22,"time":5}
But when updating the data via Laravel like this:
$json_data = json_encode(['total' => 22, ...]);
$table->where->update(['column->data' => $json_data])
the result is as follows:
{"data": "{\"total\":22,\"time\":5}"}
When I pass the array without json_encode
ing it, it raises an error instead.
How can I assign a non-primitive value directly to a property in a JSON field?
CodePudding user response:
I think the only solution is as follows:
$data = ['total' => 22, 'time' => 5];
$table->where->update([
'column->data->total' => $data['total'] ,
'column->data->time' => $data['time']
])
I searched to batch update the contents of a json key without double quotes but couldn't find it.
Either the content will be pulled completely and then edited and sent again. Or individual updates will be made as above. Please let me know if you find a better solution.
CodePudding user response:
seems it got double json encoded, first you json_encode'ed it manually, then the $table->where->update-function json_encode'ed it again. try replacing
$json_data = json_encode(['total' => 22, ...]);
with just
$json_data = ['total' => 22, ...];