Laravel - 扩展模型 $guarded from parent model
Laravel - Extend Model $guarded from parent model
我有一个 BaseModel
class 扩展了 Illuminate\Database\Eloquent\Model
:
class BaseModel extends Model
{
protected $guarded = ['id', 'company_id', 'identifier'];
}
在我的其他模型 classes 中,我扩展了 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);
}
使用 GuardsAttributes 特征函数已经存在于 baseModel
public function __construct(array $attributes = array())
{
$this->guard(array_merge($this->getGuarded(), ['address', 'phone']));
parent::__construct($attributes);
}
我有一个 BaseModel
class 扩展了 Illuminate\Database\Eloquent\Model
:
class BaseModel extends Model
{
protected $guarded = ['id', 'company_id', 'identifier'];
}
在我的其他模型 classes 中,我扩展了 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);
}
使用 GuardsAttributes 特征函数已经存在于 baseModel
public function __construct(array $attributes = array())
{
$this->guard(array_merge($this->getGuarded(), ['address', 'phone']));
parent::__construct($attributes);
}