Skip to content

Latest commit

 

History

History
43 lines (37 loc) · 858 Bytes

eloquent-calendar-days.md

File metadata and controls

43 lines (37 loc) · 858 Bytes

Eloquent Calendar Days

Build a calendar of entries for every day in the current month.

<?php
[
    [
     "date" => "05/01/2020",
     "entries" => [...],
    ],
    [
     "date" => "05/02/2020",
     "entries" => [...],
    ],
]
<?php
// Calendar Details
$end = now()->endOfMonth();
$start = now()->startOfMonth();
$length = now()->daysInMonth;

// Calendar Query
$pages = App\Model::query()
    ->whereBetween('created_at', [$start, $end])
    ->take(10)
    ->get();

// Map Entries to Day
Collection::make(range(1, $length))->map(function ($days) use ($pages) {
    $now = now()->startOfMonth()->addDays($days - 1);
    return [
        'date' => $now->format('m/d/Y'),
        'entries' => $pages->filter(function ($model) use ($now) {
            return optional($model->created_at)->isSameDay($now);
        })
    ];
});