Created
October 26, 2023 15:33
-
-
Save MrPunyapal/9d3bede85475faa387073129a6126c3c to your computer and use it in GitHub Desktop.
Did you know you can pass a closure in the GroupBy method of a collection? 🤯
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$posts = Post::filter($filters)->get(); | |
$groupBy = $filters['groupBy'] ?? 'week'; | |
$posts->groupBy(function ($post) use ($groupBy) { | |
return match ($groupBy) { | |
'day' => $post->published_at->format('D d M Y'), | |
// 📅 Group by day, format 'Day Date Month Year' | |
'week' => $post->published_at->copy()->startOfWeek()->format('d M Y') | |
. ' - ' . $post->published_at->copy()->endOfWeek()->format('d M Y'), | |
// 📆 Group by week, format 'Start Date - End Date' | |
'month' => $post->published_at->format('M Y'), | |
// 🗓️ Group by month, format 'Month Year' | |
'year' => $post->published_at->format('Y'), | |
// 📆 Group by year, format 'Year' | |
'quarter' => $post->published_at->copy()->firstOfQuarter()->format('M') | |
. ' - ' . $post->published_at->copy()->lastOfQuarter()->format('M Y'), | |
// 📊 Group by quarter, format 'Start Month - End Month Year' | |
}; | |
}) | |
// some examples of outputs... | |
// quarter | |
array:2 [▼ // routes\web.php:115 | |
"Jul - Sep 2023" => Illuminate\Database\Eloquent\Collection {#365 ▶} | |
"Apr - Jun 2023" => Illuminate\Database\Eloquent\Collection {#364 ▶} | |
"Jan - Mar 2023" => Illuminate\Database\Eloquent\Collection {#363 ▶} | |
"Oct - Dec 2022" => Illuminate\Database\Eloquent\Collection {#362 ▶} | |
] | |
// year | |
array:2 [▼ // routes\web.php:115 | |
"2023" => Illuminate\Database\Eloquent\Collection {#365 ▶} | |
"2022" => Illuminate\Database\Eloquent\Collection {#364 ▶} | |
] | |
// week | |
array:2 [▼ // routes\web.php:115 | |
"01 Jan 2023 - 07 Jan 2023" => Illuminate\Database\Eloquent\Collection {#365 ▶} | |
"08 Jan 2023 - 14 Jan 2023" => Illuminate\Database\Eloquent\Collection {#364 ▶} | |
"15 Jan 2023 - 21 Jan 2023" => Illuminate\Database\Eloquent\Collection {#363 ▶} | |
"22 Jan 2023 - 28 Jan 2023" => Illuminate\Database\Eloquent\Collection {#362 ▶} | |
] | |
// month | |
array:2 [▼ // routes\web.php:115 | |
"Jan 2023" => Illuminate\Database\Eloquent\Collection {#365 ▶} | |
"Dec 2022" => Illuminate\Database\Eloquent\Collection {#364 ▶} | |
"Nov 2022" => Illuminate\Database\Eloquent\Collection {#363 ▶} | |
"Oct 2022" => Illuminate\Database\Eloquent\Collection {#362 ▶} | |
] | |
// day | |
array:2 [▼ // routes\web.php:115 | |
"Sun 01 Jan 2023" => Illuminate\Database\Eloquent\Collection {#365 ▶} | |
"Sat 31 Dec 2022" => Illuminate\Database\Eloquent\Collection {#364 ▶} | |
"Fri 30 Dec 2022" => Illuminate\Database\Eloquent\Collection {#363 ▶} | |
"Thu 29 Dec 2022" => Illuminate\Database\Eloquent\Collection {#362 ▶} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment