简体   繁体   English

Laravel(5.3)采摘集合返回除数组以外的其他结果

[英]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. 我正在做一个选择框,并且在数据库模型上使用了pluck方法。

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. 如您所见,该ID不存在。

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? 让我觉得这是var dumper中的错误吗?

@Patrick Vd Pols @帕特里克Vd Pols

could you please try as below 你可以尝试如下

Organisation::pluck('name','id')->all();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM