Laravel 具有2个以上参数的Pulk函数

Laravel 具有2个以上参数的Pulk函数,laravel,eloquent,Laravel,Eloquent,大家好,我尝试在雄辩的上使用函数查询显示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_license',有人知道怎么做吗

大家好,我尝试在雄辩的上使用函数查询显示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_license'
,有人知道怎么做吗

您可以将数组传递给
get()
方法:

$licence_entraineur = Licencies::where(['structure_id' => Auth::user()->structure->id])
    ->where('type_licence_id', '1')
    ->get(['lb_nom', 'num_licence', 'id']);
$licence_entraineur = Licencies::select('lb_nom', 'num_licence', 'id')
    ->where(['structure_id' => Auth::user()->structure->id])
    ->where('type_licence_id', '1')
    ->get();
或者使用
select()
方法:

$licence_entraineur = Licencies::where(['structure_id' => Auth::user()->structure->id])
    ->where('type_licence_id', '1')
    ->get(['lb_nom', 'num_licence', 'id']);
$licence_entraineur = Licencies::select('lb_nom', 'num_licence', 'id')
    ->where(['structure_id' => Auth::user()->structure->id])
    ->where('type_licence_id', '1')
    ->get();
如果愿意,可以对集合使用
map()
方法:

$licence_entraineur->map(function($i) {
    return array_values((array)$i);
});
更新

在评论中,您说过希望得到混合的结果,因此请使用以下代码(在5.4中有效,在5.3中无效):

还有,你可以在这里用。比如:

public function getTitleAttribute($value)
{
    return $this->lb_nom.' - '.$this->num_licence.' - '.$this->lb_prenom;
}

并将其用作pull('title','id')

pull函数创建和创建数组。php数组由
索引
组成。如果使用
->pulk(arg1)
索引将是升序整数,值将是arg1,如果使用
->pulk(arg1,arg2)
索引将是arg2,值将是arg1。您可能需要
->选择(列…
。谢谢,它正在工作!在我的“选择刀片”视图中,我也会得到这些值。但是我得到的结果像{“lb_nom:mourareau”,lb_prenom:mathieu…有一种方法可以只获取值而不获取列名?@MathieuMourareau是的,只需链接
映射()
method,如我在回答的第三部分所示。我用你的代码尝试了这个链,但现在我调用了未定义的method illumb\Database\Query\Builder::map(),但我的选择框中有{“lb_nom:mourareau,“lb_prenom”:mathieu….@MathieuMourareau我已经添加了适合你的代码。