Laravel@5.8.33
mongoDB@4.0.10
あるEloquent ORMのネストされたデータdetails['description']をアップデートしたい。
$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: