laravel 间接修改重载属性 App\Dossier::$program 无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49624156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Indirect modification of overloaded property App\Dossier::$program has no effect
提问by David
Good Day I have this code on backend (trying to update this value in MONGO) http://prntscr.com/j03gh4
美好的一天我在后端有这个代码(试图在 MONGO 中更新这个值) http://prntscr.com/j03gh4
$dossier=Dossier::where('_id',(int)$request->input('dossier_id'))->first();
//var_dump($request->input('value'));
$dossier->program[$request->input('program')]['cities']
[$request->input('city')]['services']
[$request->input('service')][$request->input('name')]=$request->input('value');
$dossier->save();
But I receive this Exception http://prntscr.com/j03h0s
但我收到此异常 http://prntscr.com/j03h0s
Indirect modification of overloaded property App\Dossier::$program has no effect
间接修改重载属性 App\Dossier::$program 无效
What have I do to repair this situation?
我该怎么做才能修复这种情况?
回答by apokryfos
The problem is that calling $dossier->program
does not actually access the property directly in Eloquent type models but rather calls a __get
method.
问题在于,$dossier->program
在 Eloquent 类型模型中,调用实际上并没有直接访问属性,而是调用了一个__get
方法。
That get method does not return a reference to the property. What you should do is grab the original property, modify it and then put it back:
该 get 方法不返回对该属性的引用。您应该做的是获取原始属性,对其进行修改然后将其放回:
$dossier=Dossier::where('_id',(int)$request->input('dossier_id'))->first();
$originalProgram = $dossier->program;
$originalProgram[$request->input('program')]['cities'][$request->input('city')]['services'][$request->input('service')][$request->input('name')]=$request->input('value');
$dossier->program = $originalProgram;
$dossier->save();