Skip Navigation

Laravel Collection GroupBy with Count

$events = CalendarEvent::where('type','interview')
  ->get()
  ->map(function ($event) {
    	$event['length'] = $event->start->diffInMinutes($event->end);
    	return $event;
    })
  ->groupBy('length')
  ->transform(function($group, $key){
        return [
            'name' => $key .' Minutes',
            'count' => $group->count()
        ];	
  })->values();

Related Snippets

See all