use Carbon\Carbon;
$notes = Note::select('id', 'created_at')->orderBy('created_at', 'desc')->get();
$noteCounts = [];
foreach ($notes as $post) {
$date = Carbon::parse($post->created_at)->format('Y-m');
// 按年和月分组统计文章数
$year = Carbon::parse($post->created_at)->format('Y');
$month = Carbon::parse($post->created_at)->format('m');
if (!isset($noteCounts[$year])) {
$noteCounts[$year] = []; // 初始化年份数组
}
if (!isset($noteCounts[$year][$month])) {
$noteCounts[$year][$month] = 0; // 初始化月份数组并设置为0
}
$noteCounts[$year][$month]++; // 月份文章数加一
}
结果 $noteCounts