Laravel Database 操作 PHP Notice: Indirect modification of overloaed property ...

Laravel@5.8.33

mongoDB@4.0.10

あるEloquent ORMのネストされたデータdetails['description']をアップデートしたい。

f:id:monteecristoo:20190908043642p:plain

$target = App\ModelName::first()

$target->details['description']

  >>> "test"

と返るので直接

$target->details['description'] = "updated"

$target->save()

をしようとすると、PHP Notice: Indirect modification of overloaed property ...

とエラーが出る。

ネストされていない項目に関しては上記でアップデートできる。例えばdetails自体はネストされていないので

$target->details = "missing child"

$target->save()

$target->details

  >>> "missing child"

 

solution: array_replaceを使用

$newValue = ['description' => 'I'm new value']

$target->details = array_replace($target->details, $newValue)

$target->save()

 

ref: 

https://stackoverflow.com/questions/34467076/indirect-modification-of-overloaded-property-laravel-mongodb