Pun*_*jar 20 concat laravel pluck laravel-5.3
我正在使用Laravel.5.3以下是我的查询
$ProjectManagers = Employees::where("designation" , 1)
->pluck(DB::raw('CONCAT(first_name," ",last_name) AS name'),'id');
Run Code Online (Sandbox Code Playgroud)
这引发了一个错误
isset或empty中的非法偏移类型
我可以知道这是否是正确的方法?
如果我不使用联系和使用像
$ProjectManagers = Employees::where("designation" , 1)->pluck('first_name','id');
Run Code Online (Sandbox Code Playgroud)
哪个工作正确并给我结果
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[8] => Punit
)
)
Run Code Online (Sandbox Code Playgroud)
预期结果 :
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[8] => Punit Gajjar
)
)
Run Code Online (Sandbox Code Playgroud)
其中第一个名称和姓氏连接在一起.
mar*_*n87 36
尝试将雄辩查询更改为:
$ProjectManagers = Employees::select(
DB::raw("CONCAT(first_name,' ',last_name) AS name"),'id')
->where('designation', 1)
->pluck('name', 'id');
Run Code Online (Sandbox Code Playgroud)
小智 33
最优雅的解决方案是创建一个存取器.
打开Employees类(模型)并添加一个访问器函数:
public function getFullNameAttribute()
{
return $this->first_name . ' ' . $this->last_name;
}
Run Code Online (Sandbox Code Playgroud)
之后,只需使用:
$ProjectManagers = Employees::where('designation', 1)->get()->pluck('full_name', 'id');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15135 次 |
| 最近记录: |