Laravel 为模型中的自定义特性赋值
如何为我的自定义属性赋值?我来自Laravel 为模型中的自定义特性赋值,laravel,Laravel,如何为我的自定义属性赋值?我来自Yii2background,有相当直接的背景。在这里,我做了这个: protected $appends = ['sites']; public function getSitesAttribute() { return $this->sites; } public function setSitesAttribute($value) {
Yii2
background,有相当直接的背景。在这里,我做了这个:
protected $appends = ['sites'];
public function getSitesAttribute()
{
return $this->sites;
}
public function setSitesAttribute($value)
{
$this->attributes['sites'] = $value;
}
尝试分配like
mymodel->sites=['1',2',3']
但未成功。尝试获取mymodel->sites
值后,抛出错误:未定义的属性“sites”
问题在于$this->sites
。如果类实例没有属性$sites
,它将抛出一个异常,这是正确的
class MyModel extends Model
{
public function getSitesAttribute()
{
return $this->attributes['sites'];
}
public function setSitesAttribute($value)
{
$this->attributes['sites'] = $value;
}
}
看看模型的方法
。您可以在这里看到正在进行getAttribute($key)
调用。深入研究,您会发现,在某个时刻,以下代码正在该方法中执行:
if($this->hasGetMutator($key)){
返回$this->mutateAttribute($key,$value);
}
这段代码调用您的getSitesAttribute
方法
您将看到,它试图通过调用getAttributeFromArray($key)
从属性数组中检索$key
(sites
)的值。这将返回存储在attributes
数组中的值,该数组是setSiteAttribute
mutator存储该值的位置
我可以想出以下选项:
$attributes
数组中检索值protected$appends=['sites'];
公共函数集合站点属性($value)
{
$this->attributes['sites']=$value;
}
$sites
属性中并从中检索。不确定$appends
是否仍然有效protected$appends=['sites'];
私人$sites=[];
公共函数getSitesAttribute()
{
返回$this->sites;
}
公共函数集合站点属性($value)
{
$this->sites=$value;
}
问题在于$this->sites
。如果类实例没有属性$sites
,它将抛出一个异常,这是正确的
class MyModel extends Model
{
public function getSitesAttribute()
{
return $this->attributes['sites'];
}
public function setSitesAttribute($value)
{
$this->attributes['sites'] = $value;
}
}
看看模型的方法
。您可以在这里看到正在进行getAttribute($key)
调用。深入研究,您会发现,在某个时刻,以下代码正在该方法中执行:
if($this->hasGetMutator($key)){
返回$this->mutateAttribute($key,$value);
}
这段代码调用您的getSitesAttribute
方法
您将看到,它试图通过调用getAttributeFromArray($key)
从属性数组中检索$key
(sites
)的值。这将返回存储在attributes
数组中的值,该数组是setSiteAttribute
mutator存储该值的位置
我可以想出以下选项:
移除访问器,让模型从$attributes
数组中检索值
protected$appends=['sites'];
公共函数集合站点属性($value)
{
$this->attributes['sites']=$value;
}
将值存储在$sites
属性中并从中检索。不确定$appends
是否仍然有效
protected$appends=['sites'];
私人$sites=[];
公共函数getSitesAttribute()
{
返回$this->sites;
}
公共函数集合站点属性($value)
{
$this->sites=$value;
}
您的访问者不太正确,请注意,您没有返回$this->attributes['sites']代码>但改为$this->sites
class MyModel extends Model
{
public function getSitesAttribute()
{
return $this->attributes['sites'];
}
public function setSitesAttribute($value)
{
$this->attributes['sites'] = $value;
}
}
然后,
您可能需要为模型构造函数中的$this->attributes['sites']
设置初始默认值。您的访问器不太正确,请注意,您没有返回$this->attributes['sites']代码>但改为$this->sites
class MyModel extends Model
{
public function getSitesAttribute()
{
return $this->attributes['sites'];
}
public function setSitesAttribute($value)
{
$this->attributes['sites'] = $value;
}
}
然后,
您可能需要为模型构造函数中的$this->attributes['sites']
设置初始默认值。检查公共函数getSitesAttribute(){dd($this->attributes['sites']);}
检查公共函数getSitesAttribute(){dd($this->attributes['sites']);}
另一个选项是使用protected$attributes=['sites'=>[]]]
而不是构造函数。另一个选项是使用protected$attributes=['sites'=>[]]
而不是构造函数。