【发布时间】:2021-06-01 09:30:10
【问题描述】:
我尝试了多种方法在模型中调用下面的方法
public function getFullNameAttribute()
{
return "{$this->first_name} {$this->last_name}";
}
我尝试了什么:
User::select('first_name', 'last_name')
->with([
'fullName' => function($query){
$query->select(DB::raw("CONCAT(first_name, ' ', last_name) AS full_name"));
}
])
->where('user_id', $id)
->where('is_deleted', 0)
->first();
我尝试的另一种方法:
User::select(DB::raw("CONCAT(first_name, ' ', last_name) AS full_name"))
->with([
'fullName' => function($query){
$query->select('firstname', 'lastname');
}
])
->where('user_id', $id)
->where('is_deleted', 0)
->first();
但没有任何东西在调用 get<>Attribute 名称。
【问题讨论】:
-
为什么要混用下划线?
firstname和first_name- 是哪个?
标签: php laravel laravel-8 accessor