Php Laravel-从父模型扩展模型$

Php Laravel-从父模型扩展模型$,php,laravel,laravel-5,eloquent,laravel-6,Php,Laravel,Laravel 5,Eloquent,Laravel 6,我有一个BaseModel类,它扩展了illumb\Database\elount\Model: class BaseModel extends Model { protected $guarded = ['id', 'company_id', 'identifier']; } 在我的其他模型类中,我扩展了BaseModel,并试图从子模型中的BaseModel扩展$guarded: class Person extends BaseModel { public functio

我有一个
BaseModel
类,它扩展了
illumb\Database\elount\Model

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


使用baseModel上已有的GuardsAttributes特性函数

public function __construct(array $attributes = array())
{
    $this->guard(array_merge($this->getGuarded(), ['address', 'phone']));
    parent::__construct($attributes);
}

你好谢谢你的帮助,它现在起作用了,但就是这样:`$person=newperson()$person->fill($request->all());`但不是这样工作的:`$person=newperson($request->all())//这样它只保护父构造函数`但我认为这是唯一的方法,因为它首先调用父构造函数,然后用
$this->guard(…)
更改保护。更新:我刚刚更改了构造函数的顺序,首先调用
$this->guard()
,然后调用
父构造函数::\uu construct()
,它成功了!非常感谢你!
public function __construct(array $attributes = array())
{
    $this->guard(array_merge($this->getGuarded(), ['address', 'phone']));
    parent::__construct($attributes);
}