【发布时间】:2020-09-24 16:37:32
【问题描述】:
我有一个扩展 Illuminate\Database\Eloquent\Model 的 BaseModel 类:
class BaseModel extends Model
{
protected $guarded = ['id', 'company_id', 'identifier'];
}
在我的其他模型类中,我扩展了BaseModel,并尝试在子模型中从BaseModel 扩展$guarded:
class Person extends BaseModel
{
public function __construct(array $attributes = array())
{
parent::__construct($attributes);
$this->guarded = array_merge($this->guarded, ['address', 'phone']); //This is that I'm trying to do
}
}
但是,它不起作用。是否可以从父模型扩展 $guarded 或者我也需要在子模型上声明所有字段?
解决方案 - 感谢@Jignesh Joisar!
public function __construct(array $attributes = array())
{
$this->guard(array_merge($this->getGuarded(), ['address', 'phone']));
parent::__construct($attributes);
}
【问题讨论】:
标签: php laravel laravel-5 eloquent laravel-6