2 回答
TA貢獻(xiàn)1900條經(jīng)驗(yàn) 獲得超5個(gè)贊
setAnsAtribute 方法沒(méi)有代碼?如果您有代碼,請(qǐng)?zhí)砑痈啻a以查看發(fā)生了什么。謝謝你。
如果您沒(méi)有代碼,我會(huì)在下面指出。
我將您推薦給 Laravel Mutators頁(yè)面,以便您可以查看示例并附上示例代碼以及您正在嘗試執(zhí)行的操作。
función pública setAnsAttribute ($value) {
/**
* You can transform the value or modify other values in the table here,
* according to your needs.
*/
$this->attributes['ans'] = $value;
}
TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個(gè)贊
我從來(lái)沒(méi)有遇到過(guò)這個(gè)錯(cuò)誤,所以我做了一些研究并創(chuàng)建了以下內(nèi)容來(lái)說(shuō)明這個(gè)問(wèn)題:
班上:
class SomeMagicMethodImplementer
{
private $properties = [];
public function __get($k)
{
return $this->properties[$k] ?? null;
}
public function __set($k, $v)
{
$this->properties[$k] = $v;
}
}
用法:
$impl = new SomeMagicMethodImplementer();
$impl->bar = 'bar';
print_r($impl->bar); // prints bar
$impl->foo[] = 'foo';
錯(cuò)誤原因:
$impl->foo[] = 'foo' // Will throw error: Indirect modification of overloaded property
間接修改重載屬性
使用上面的示例代碼,該錯(cuò)誤基本上指出經(jīng)由魔設(shè)定器創(chuàng)建的任何屬性可以僅通過(guò)專(zhuān)用實(shí)例變量進(jìn)行修改$properties。
在 Laravel 和模型的上下文中,屬性只能通過(guò) protected 實(shí)例變量進(jìn)行修改$attributes。
Laravel 示例:
public function setAnsAttribute($value)
{
// output will be an array
$this->attributes['ans'][] = $value;
}
- 2 回答
- 0 關(guān)注
- 203 瀏覽
添加回答
舉報(bào)
