【问题标题】:after filtering a plucked laravel collection, the indexed array change to Associative array过滤采摘的 laravel 集合后,索引数组更改为关联数组
【发布时间】:2018-05-18 05:55:17
【问题描述】:

我有一个雄辩的模型集合,例如user模型,我使用pluck方法从这个集合中获取唯一的post_id,这个方法给了我indexed array of post_id,但是当我使用@987654325这个indexed array 的@ 方法将结果更改为Associative array。我不想要assoc array 结果。我只想要indexed array 中唯一的post_id。 laravel 自动改变我的结果。

$this->posts->pluck('post_id')->unique('post_id')  

结果是:{ "1": 1 , "2": 2 }

这可能是一个错误还是我在通过方法获取数据时出错了?

【问题讨论】:

    标签: laravel collections


    【解决方案1】:

    你可以像这样使用groupBy

    $this->posts->groupBy('post_id')->pluck('post_id');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-07
      • 2020-01-25
      • 2020-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多