0

In Laravel 5 with MongoDB and jenssegers I'm trying to update an document's Array field. But it's not updating.

When field contains Array[2] and I'm trying to update it with Array[1] it's not updating. In otherhand another document fields are updating properly.

I checked scenarios:

old value | new value | result
---------------------------------
Array(1)  | Array(2)  | OK
---------------------------------
Array(1)  | null      | OK
---------------------------------
Array(1)  | Array(0)  | OK
---------------------------------
Array(2)  | Array(1)  | NO UPDATE

My document looks like this:

{
    "name": "test",
    "default": 0,
    "variables": [
        {
            "name": "responsibilities",
            "type": "text"
        },
        {
            "name": "requirements",
            "type": "text"
        }
    ]
}

And my PHP code (I dealt with this in that way for now, but it's not elegant at all). What am I missing?

$document->name = $name;
$document->default = $default;
$document->variables = [];  // there is neccesity to clear this field first, because overriding with smaller array
$document->save();          // is faulty (for ex. when removing one variable)
$document->variables = $variables;
$document->save();
5
  • you can do some kind of setter like this laraveldaily.com/… Commented May 7, 2019 at 13:29
  • You're right, but that is just moving this one more db operation to another place. Commented May 8, 2019 at 6:23
  • qcode.in/use-mysql-json-field-in-laravel check i think you can pass array to the update method Commented May 8, 2019 at 13:43
  • @DominikDominik have you got a solution? I'm also experiencing similar issue stackoverflow.com/questions/58074224/… Commented Sep 24, 2019 at 9:55
  • unfortunately I didn't found a solution. There is still @fixme comment in my code. Commented Sep 24, 2019 at 12:58

0

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.