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

0 Answers0