Groupby为特定类型的记录

时间:2017-11-17 13:42:29

标签: mysql laravel

我对查询感到困惑。我有一个带有{type}字段的帖子表.type字段有不同的值。现在的要求是,我想获取特定类型的帖子使用groupBy {Date}。

    Collection {#832 ▼
  #items: array:5 [▼
    0 => Post {#833 ▶}
    1 => Post {#834 ▶}
    2 => Post {#835 ▶}
    3 => Post {#836 ▶}
    4 => Post {#837 ▼
      Post: array:5 [▼
      0 => Post {#833 ▶}
      1 => Post {#834 ▶}
      2 => Post {#835 ▶}
      3 => Post {#836 ▶}
      ]
    }
  ]
}
第一个数组中的

第四个键是groupBy。 我希望你能理解我的要求 在此先感谢

1 个答案:

答案 0 :(得分:0)

尝试类似:

$type = "example"; //whatever a valid value for type would by, based on your code.
Post::where('type', $type)->groupBy('date')->get();

where()放在groupBy()之前,您可以对结果进行过滤。