0

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()

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.