Indirect Modification of Overloaded Property Larav

2019-03-18 06:57发布

问题:

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.

回答1:

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


回答2:

Store your variable in a temp variable:

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

// Works fine
$tmpArray = $this->someProperty;
$foo = array_shift($tmpArray);


回答3:

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!



回答4:

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;