2

Hello everyone i try to display more than 2 param with a pluck function query on Eloquent .

here my query :

$licence_entraineur = Licencies::where(['structure_id' => Auth::user()->structure->id])->where('type_licence_id' , '1')->pluck('lb_nom' , 'num_licence' , 'id');

I just get the 'lb_nom' , i would like also 'num_licence' , someone knows how to do this ?

manniL
  • 7,157
  • 7
  • 46
  • 72
Mathieu Mourareau
  • 1,140
  • 2
  • 23
  • 47
  • Pluck function creates and array. A php array consists of `indexes` and `values`. If you use `->pluck(arg1)` the indexes will be ascending integers, values will be arg1 and if you use `->pluck(arg1, arg2)` the indexes will be arg2 and values will be arg1. You might want `->select(columns...)` instead. – DevK Mar 24 '17 at 10:30

1 Answers1

2

You can pass an array to the get() method:

$licence_entraineur = Licencies::where(['structure_id' => Auth::user()->structure->id])
    ->where('type_licence_id', '1')
    ->get(['lb_nom', 'num_licence', 'id']);

Or use the select() method:

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

If you want to get pluck() like array without keys, you can use the map() method on a collection:

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

UPDATE

In the comments you've said you want to get mixed result, so use this code (works in 5.4, doesn't work in 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];
    });

Also, you could use an accessor here. Something like:

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

And use it as ->pluck('title', 'id')

Community
  • 1
  • 1
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • thanks it's working ! in my select blade view i get the values as well . but i get the result like {"lb_nom : mourareau , "lb_prenom": mathieu .... there is a method to get only the values and not the column names ? – Mathieu Mourareau Mar 24 '17 at 10:32
  • @MathieuMourareau yes, just chain the `map()` method as I've shown in 3rd part of my answer. – Alexey Mezenin Mar 24 '17 at 10:33
  • i tried the chain with your code but now i get Call to undefined method Illuminate\Database\Query\Builder::map() – Mathieu Mourareau Mar 24 '17 at 10:39
  • but i steel have in my select box {"lb_nom : mourareau , "lb_prenom": mathieu .... – Mathieu Mourareau Mar 24 '17 at 10:41
  • my code is now : $licence_entraineur = Licencies::select('lb_nom' , 'num_licence' , 'id') ->where(['structure_id' => Auth::user()->structure->id]) ->where('type_licence_id' , '1') ->map(function($i) { return array_values((array)$i); }); – Mathieu Mourareau Mar 24 '17 at 10:44
  • but i get Call to undefined method Illuminate\Database\Query\Builder::map() – Mathieu Mourareau Mar 24 '17 at 10:44
  • You've forgot to use `get()`. Use the code from 1st or 2nd part of my answer and chain the code from 3rd part of the answer if you want to get just values. Like `....->get()->map(....` – Alexey Mezenin Mar 24 '17 at 10:45
  • like this .? $licence_entraineur = Licencies::where(['structure_id' => Auth::user()->structure->id]) ->where('type_licence_id' , '1') ->get(['lb_nom' , 'num_licence' , 'id'])->map(function($i) { return array_values((array)$i); }); Now i have htmlentities() expects parameter 1 to be string, array given – Mathieu Mourareau Mar 24 '17 at 10:49
  • it's a problem with my select blade . actually i have this : {!! Form::select('licence_entraineur_id', $licence_entraineur , null, ['class' => 'form-control', 'placeholder' => 'Selectionnez un Entraineur']) !!} – Mathieu Mourareau Mar 24 '17 at 10:52
  • @MathieuMourareau for the `Form::select()` you need to use `pluck()` with two parameters. It will create an array with `['2nd parameter' => '1st parameter']` data structure. – Alexey Mezenin Mar 24 '17 at 11:16
  • ok so i forget the code that you sended to me ? it need to be a pluck to use a blade select box right ? – Mathieu Mourareau Mar 24 '17 at 11:20
  • @MathieuMourareau yes. – Alexey Mezenin Mar 24 '17 at 11:21
  • How i can do this with pluck ? sorry i'm lost ^^ – Mathieu Mourareau Mar 24 '17 at 11:28
  • @MathieuMourareau it depends on what `select` list you want to see. What do you want to use as keys? IDs I guess? And what do you want to make list option title (one item title) look like? `num_licence`? – Alexey Mezenin Mar 24 '17 at 11:31
  • I would like to have a list with 'lb_nom' , 'lb_prenom' , 'num_licence' and 'id' but i would like to see only the values in the select box not the columns of the database – Mathieu Mourareau Mar 24 '17 at 11:32
  • @MathieuMourareau so, you want a list with 4 items? Or you want 4 lists? Or you want one list where ID is the key and item title is something like 'lb_nom - lb_prenom - num_licence'? I'm sorry, but it's not clear. – Alexey Mezenin Mar 24 '17 at 11:35
  • I would like to get all the list of items in the select box with values item title like 'lb_nom - lb_prenom - num_licence' yes – Mathieu Mourareau Mar 24 '17 at 11:41
  • for exemple if i click on the select box i would like to see Mourareau Mathieu 1458575 but not {"lb_nom : mourareau , "lb_prenom" : mathieu , "num_licence " : 1458575 – Mathieu Mourareau Mar 24 '17 at 11:41
  • 1
    @MathieuMourareau I've added the code which should work for you. – Alexey Mezenin Mar 24 '17 at 11:48