无法根据 Laravel 7.x 文档中的 Eloquent 访问器 给出的例子完成属性值的计算。
laravel 7.x 文档中的访问器的demo中
/**
* 获取用户的姓名.
*
* @return string
*/
public function getFullNameAttribute()
{
//这里直接使用$this->first_name会报错
return "{$this->first_name} {$this->last_name}";
}
但是使用
public function getFullNameAttribute()
{
//这理使用attributes就可以获取到期待的值
return "{$this->attributes['first_name]'} {$this->attributes['last_name']}";
}
不知道是否是文档中给的demo代码是不是有问题的?
我看不懂你问的问题
使用 $this->first_name 报了什么错误?
访问器不需要加大括号啊,报错应该贴出来看看.