Indirect Modification of Overloaded Property Larav

2019-03-18 06:41发布

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.

4条回答
Root(大扎)
2楼-- · 2019-03-18 07:16

Don't try to directly modify an Eloquent collection, instead cast the collection to an Array and use that array for your modifications.

$model = Model::findOrFail($id);

$array = $model->toArray();
$array['key'] = $value;
查看更多
戒情不戒烟
3楼-- · 2019-03-18 07:18

You can use this method in case that you want to add a single item to the array:

PHP:

$new_id = "1234567";
$object->array_ids = array_merge($object->array_ids, [$new_id]);

This work's for me!

查看更多
Rolldiameter
4楼-- · 2019-03-18 07:28

Store your variable in a temp variable:

// Fatal error    
$foo = array_shift($this->someProperty);

// Works fine
$tmpArray = $this->someProperty;
$foo = array_shift($tmpArray);
查看更多
啃猪蹄的小仙女
5楼-- · 2019-03-18 07:29

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'));
查看更多
登录 后发表回答