Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 为模型中的自定义特性赋值_Laravel - Fatal编程技术网

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'=>[]]
    而不是构造函数。