大家好,我尝试在 Eloquent 上使用 pluck 功能查询显示2个以上的参数。
这里是我的查询:
$licence_entraineur = Licencies::where(['structure_id' => Auth::user()->structure->id])->where('type_licence_id' , '1')->pluck('lb_nom' , 'num_licence' , 'id');
我刚收到'lb_nom'
,我还想'num_licence'
,有人知道怎么做?
答案 0 :(得分:2)
您可以将数组传递给get()
方法:
$licence_entraineur = Licencies::where(['structure_id' => Auth::user()->structure->id])
->where('type_licence_id', '1')
->get(['lb_nom', 'num_licence', 'id']);
或使用select()
方法:
$licence_entraineur = Licencies::select('lb_nom', 'num_licence', 'id')
->where(['structure_id' => Auth::user()->structure->id])
->where('type_licence_id', '1')
->get();
如果您想要get pluck()
like array without keys,可以对集合使用map()
方法:
$licence_entraineur->map(function($i) {
return array_values((array)$i);
});
<强>更新强>
在评论中你说你想得到混合结果,所以使用这段代码(在5.4中工作,在5.3中不起作用):
$licence_entraineur = Licencies::select('lb_nom', 'num_licence', 'lb_prenom', 'id')
->where(['structure_id' => Auth::user()->structure->id])
->where('type_licence_id' , '1')
->get()
->mapWithKeys(function($i) {
return [$i->id => $i->lb_nom.' - '.$i->num_licence.' - '.$i->lb_prenom];
});
此外,您可以在此处使用an accessor。类似的东西:
public function getTitleAttribute($value)
{
return $this->lb_nom.' - '.$this->num_licence.' - '.$this->lb_prenom;
}
并将其用作->pluck('title', 'id')