我正在使用第三方表,它有姓名字段,非常烦人。名字进入名字,当然姓氏进入姓氏。
我想在模型中将它们连接在一起,因为我需要多次使用全名,所以我添加了:
public function getFullNameAttribute() : Attribute
{
return new Attribute(
$this->name . ' ' . $this->surname
);
}
然后添加
protected $appends = ['full_name'];
我认为这总是会显示我的 concat 字段,但是当我添加表时它不在那里。
要在 Laravel 中为属性定义访问器,您需要这样做并从声明中删除类型提示 Attribute 或将其替换为 string。
public function getFullNameAttribute()
{
return $this->name . ' ' . $this->surname;
}
您可以在模型中创建访问器方法。
public function getFullName()
{
return $this->name.' '.$this->surname;
}