2 回答

TA貢獻(xiàn)1900條經(jīng)驗(yàn) 獲得超5個贊
setAnsAtribute 方法沒有代碼?如果您有代碼,請?zhí)砑痈啻a以查看發(fā)生了什么。謝謝你。
如果您沒有代碼,我會在下面指出。
我將您推薦給 Laravel Mutators頁面,以便您可以查看示例并附上示例代碼以及您正在嘗試執(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個贊
我從來沒有遇到過這個錯誤,所以我做了一些研究并創(chuàng)建了以下內(nèi)容來說明這個問題:
班上:
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';
錯誤原因:
$impl->foo[] = 'foo' // Will throw error: Indirect modification of overloaded property
間接修改重載屬性
使用上面的示例代碼,該錯誤基本上指出經(jīng)由魔設(shè)定器創(chuàng)建的任何屬性可以僅通過專用實(shí)例變量進(jìn)行修改$properties。
在 Laravel 和模型的上下文中,屬性只能通過 protected 實(shí)例變量進(jìn)行修改$attributes。
Laravel 示例:
public function setAnsAttribute($value)
{
// output will be an array
$this->attributes['ans'][] = $value;
}
- 2 回答
- 0 關(guān)注
- 180 瀏覽
添加回答
舉報