![](/img/trans.png)
[英]Laravel: how to retrieve all instances of child models which extend from parent model?
[英]Laravel - Extend Model $guarded from parent model
我有一個擴展Illuminate\Database\Eloquent\Model
的BaseModel
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.