我正在尝试为博客创建存档列表。我想要这样的东西:
2020
- 一月 (3)
- 二月 (2)
2019
- 一月 (10)
- 二月 (23)
到目前为止,我已经设法将帖子整理到一个显示计数的数组中,但是该数组每个月都单独作为一个数组。我希望它被嵌套,以便年份是父数组,然后每个月都是嵌套的。
$posts_by_date = $blog->hasMany(Post::class)
->where('status', 'published')
->selectRaw('year(created_at) year, month(created_at) month, count(*) count')
->groupBy('year', 'month')
->orderByRaw('created_at') desc')
->get()
->toArray();
有没有办法让组嵌套而不是分开?我知道我可以在之后修改结果,我只是认为如果我直接从查询中得到它会更干净。