Php 如何使用laravel和carbon获得过去6个月内基于给定日期的每周的所有结果

Php 如何使用laravel和carbon获得过去6个月内基于给定日期的每周的所有结果,php,laravel,datetime,php-carbon,Php,Laravel,Datetime,Php Carbon,我每天都要创建许多记录,我想知道根据创建日期每周完成多少记录。问题可能是这样的 $records_weekly = DB::table('records') ->where('created_at', '>=', Carbon::now()->subMonths(4)) ->groupBy('week') ->get() ->toArray(); 我希望你有雄辩的: $date = Carbon::now(); $recor

我每天都要创建许多记录,我想知道根据创建日期每周完成多少记录。问题可能是这样的

$records_weekly = DB::table('records')

   ->where('created_at', '>=', Carbon::now()->subMonths(4))

   ->groupBy('week')

   ->get()

   ->toArray();

我希望你有雄辩的:

$date = Carbon::now();

$records_weekly = Records::all()->where('created_at', '>=', Carbon::now()->subMonths(4))
                            ->groupBy(function($date) {
                                return Carbon::parse($date->created_at)->format('W');
                            })->sortKeysDesc();
这将显示产品输出,例如,注意
20
21
22
是周数

{
    "20": [...],
    "21": [...],
    "22": [
           {
            "id": 19,
            .....
            "created_at": "2018-05-31 07:46:33",
            "updated_at": "2018-05-23 07:46:33"
           }
          ]
}

发布你的迁移,你可以考虑拥有一个带有职位的表格。我需要知道的是根据创建日期每周发布的帖子数量。例如:在第一周:10,第二周:8,第三周:40等等…我尝试了你的解决方案,但我得到了这个错误:方法sortKeysDesc不存在。基本上,我需要的是显示与输出类似的记录。例如:第一周:20,第二周:10,第三周:8等等。你可以删除
sortKeysDesc
。检查一下