Tuesday, April 4, 2017

Laravel (5.3) pluck collection is returning other results than array

I've encoutered something strange. I'm making a selectbox, and i'm using the pluck method on a database model.

This piece of code:

    $orgs = Organisation::pluck('name', 'id');
    dd($orgs);

Gives me the following results:

Collection {#611 ▼
  #items: array:6961 [▼
    0 => "Test organisatie"
    1 => "Name"
    2 => "Another"

As you can see, the ID is not present.

Now when i make it into an array:

    $orgs = Organisation::pluck('name', 'id')->toArray();
    dd($orgs);

It gives the following results:

array:6961 [▼
1 => "Test organisatie"
3 => "Name"
19 => "Another"

The array is perfectly usable, i just don't understand why there's a difference.

--Edit: When i use the collection in the select form helper, it does display the keys properly. Making me think it's a bug in the var dumper?



via Patrick Vd Pols

Advertisement