使用 Laravel Carbon 为每个月制作一系列订单

Make an array of orders for each month with Laravel Carbon

我对 Laravel Carbon 不太熟悉,我希望能够将一年中每个月的订单数量分组在 table 中。例如,如果我在 1 月份有 4 个订单,2 月份有 8 个订单,3 月份有 16 个订单,table 将如下所示:

[4,8,16,...]

我的数据库是这样的:

我搜索了很多但没有找到太多解决方案,但我知道 Laravel Carbon 是可能的...如果你能给我一个线索,我不会拒绝!

如果我做对了,它对你有用。

    $data = Table::select([
        DB::raw('count(id) as orders'),
        DB::raw('month(created_at) as month'),
    ])
    ->groupBy('month')
    ->get()
    ->toArray();