Reputation: 347
Let's say we have a user with parameters field like this:
User::first()->parameters
=> [
"pins" => [
1,
],
]
Now I want to add another pin to have something like this:
User::first()->parameters['pins']
=> [
2,
1,
]
Currently I'm doing this with the following query:
User::first()->update(['parameters' => ['pins' => array_merge([2], User::first()->parameters['pins'])]])
I was wonder if there is any Eloquent way to do that without fetching the pins first (I mean without User::first()->parameters['pins']
). Because mysql suport it with JSON_ARRAY_APPEND()
Upvotes: 0
Views: 1250
Reputation: 904
This is the only way as you did currently, to update an array of a field.
For betterment you might use an one to many
table naming like user_parameters
and use something like this $user->parameter()->create()
using eloquent relationship, to insert new value without fetching old array.
Upvotes: 1