Thanks for the tip, what calendar app is that?
Using Collection pad()
When I’ve got some time I like to read Helpers and Collections in the laravel docs.
Partly because I’m a geek and partly because it seems like sometimes I don’t get it until I have a use case.
Recently I wanted to render a calendar. I found a use for two new methods times() which skips the need for collecting a range and pad() to continue rendering elements in a blade loop even when empty.
In this first example pad let me create empty days for the start of the month (based on the day of week a month starts)...
// Carbon $date
$startDay = (int) $date->firstOfMonth()->isoFormat('d');
$daysWithOffset = $date->daysInMonth + $startDay;
$days = Collection::times($date->daysInMonth, fn($day) => match(true) {
// do stuff… the $day variable is the index starting at 1
})->pad("-$daysWithOffset", null);

And that was great in the loop, until there were not enough days to complete the last week (row) in the table. Also remember some months span 6 weeks!
pad() let me declare 7 elements even when there were only 3 days in the last week of the month.
@foreach($days->chunk(7) as $week)
<tr>
@foreach($week->pad(7, null) as $day)
<td class="border border-slate-200">
{{-- doing more cool stuff --}}
</td>
@endforeach
</tr>
@endforeach
I wonder how you use pad or what are your favorite helper/collection methods and what do you use them for?
Please or to participate in this conversation.