2017-03-24 26 views
2

大家好我嘗試顯示超過2個參數與採摘功能查詢雄辯具有超過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

普呂克函數創建和陣列。一個php數組由'索引'和'值'組成。如果使用' - > pluck(arg1)',索引將是遞增整數,則值將是arg1,如果使用' - > pluck(arg1,arg2)',則索引將爲arg2並且值爲arg1。你可能需要' - > select(columns ...)'。 – devk

回答

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); 
}); 

UPDATE

在你說你想獲得混合的結果,所以用這個代碼中的註釋(以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')

+0

感謝它的工作!在我選擇的刀片視圖中,我也獲得了這些值。但我得到的結果像{「lb_nom:mourareau,」lb_prenom「:mathieu ....有一種方法只獲取值而不是列名? –

+0

@MathieuMourareau是的,只需鏈接'map()'方法正如我在我的答案的第三部分所示 –

+0

非常感謝我現在嘗試它 –