【问题标题】:Laravel Collection group by hourLaravel 集合按小时分组
【发布时间】:2021-11-04 10:11:19
【问题描述】:

我从 MongoDB 收集:

$collection = DB::connection('test')
        ->collection('test')->get();

集合示例:

{ a: b, c: d, created_at: "2021-01-01 10:10:10" }
{ a: b, c: d, created_at: "2021-01-01 11:10:10" }

是否可以按小时分组收集? 10..11..12 等

【问题讨论】:

    标签: laravel collections


    【解决方案1】:

    我希望它有效

    $array = [
               ['a' => 'b', 'c' => 'd', 'created_at' => '2021-01-01 10:10:10'],
               ['a' => 'b', 'c' => 'd', 'created_at' => '2021-01-01 11:10:10'],
             ];
    $collect = collect($array);
    $collect = $collect->groupBy(function($collect){
         return date('H',strtotime($collect['created_at']));
    });
    dd($collect);
    

    【讨论】:

      猜你喜欢
      • 2019-06-28
      • 2021-03-08
      • 1970-01-01
      • 2018-08-20
      • 1970-01-01
      • 2019-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多