/** * Get list of latest posts grouped by desired time format * * @param QueryBuilder $query * @param string $format */ public function scopeArchiveLatest($query, $format = 'F Y') { return $query->latest()->get(['created_at'])->groupBy(function ($date) use($format) { return \Carbon\Carbon::parse($date->created_at)->format($format); }); }