簡體   English   中英

Laravel - 擴展 Model $guarded from parent model

[英]Laravel - Extend Model $guarded from parent model

我有一個擴展Illuminate\Database\Eloquent\ModelBaseModel class :

class BaseModel extends Model
{
    protected $guarded = ['id', 'company_id', 'identifier'];
}

在我的其他 Model 類中,我擴展了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
    }
}

但是,它不起作用。 是否可以從父 Model 擴展$guarded或者我也需要在子模型上聲明所有字段?

解決方案-感謝@Jignesh Joisar

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

使用GuardsAttributes特征 function 已經存在於 baseModel

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM