Indirect Modification of Overloaded Property Laravel MongoDB

- 1 answer

Ad

I am using MongoDB with Laravel. I have a collection called categories which has one document

[
  {
    "_id": "567dc4b4279871d0068b4568",
    "name": "Fashion",
    "images": "http://example.com/1.jpg",
    "specifics": [
      "made"
    ],
    "brands": [
      {
        "name": "Giordano",
        "logo": "http://example.com/"
      },
      {
        "name": "Armani",
        "logo": "http://example.com/"
      }
    ],
    "updated_at": "2015-12-25 22:40:44",
    "created_at": "2015-12-25 22:35:32"
  }
]

I am trying to make a function that add specifics to the specifics array in the above document.

Here is how my request body is

HTTP: POST
{
    "specifics": [
        "material"
    ]
}

And i am handling this request with the following function

/**
 * Add specs to category
 * @param string $category_id
 * @return Illuminate\Http\JsonResponse
 */
public function addSpecifics($category_id)
{
    $category = $this->category->findOrFail($category_id);
    $category->specifics[] = $this->request->get('specifics');
    $status_code = config('http.UPDATED');
    return response()->json($category->save(), $status_code);
}

But when i hit this call, I get error of

ErrorException in CategoryController.php line 101: Indirect modification of overloaded property App\Category::$specifics has no effect

Please help me in fixing this.

I am using https://github.com/jenssegers/laravel-mongodb this package for MongoDB.

Ad

Answer

Ad

Due to how accessing model attributes is implemented in Eloquent, when you access $category->specifics, a magic __get() method is called that returns a copy of that attribute's value. Therefore, when you add an element to that copy, you're just changing the copy, not the original attribute's value. That's why you're getting an error saying that whatever you're doing, it won't have any effect.

If you want to add a new element to $category->specifics array, you need to make sure that the magic __set() is used by accessing the attribute in a setter manner, e.g.:

$category->specifics = array_merge($category->specifics, $this->request->get('specifics'));
Ad
source: stackoverflow.com
Ad