It would be better to group your data by the month before passing data to blade template. By using collection method mapToGroups
https://laravel.com/docs/8.x/collections#method-maptogroups
$planGroups = Plan::get()->mapToGroups(function($plan, $key) {
$planDate = CarbonCarbon::parse($plan->date);
return ["{$planDate->month} {$planDate->year}" => $plan];
})
->all();
You will get several groups of plans grouped by month, e.g.
[
'11 2020' => [plan, plan, ...],
'12 2020' => [plan, plan, plan, ...]
]
Then in your blade template you can loop through the months then loop through the plans.
@foreach ($planGroups as $month => $plans)
// show month heading
@foreach ($plans as $plan)
// show each plan
@endforeach
@endforeach
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…