39 lines
1.0 KiB
PHP
39 lines
1.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Entry;
|
|
use App\Models\Event;
|
|
use App\Models\Student;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class DoublerService
|
|
{
|
|
public function doublersForEvent(Event $event)
|
|
{
|
|
$cacheKey = 'event'.$event->id.'doublers';
|
|
return Cache::remember($cacheKey, 60, function () use ($event) {
|
|
return $this->findDoublersForEvent($event);
|
|
});
|
|
}
|
|
|
|
protected function findDoublersForEvent(Event $event): array
|
|
{
|
|
$entries = $event->entries;
|
|
$entries->load('student.school');
|
|
$entries->load('audition');
|
|
$grouped = $entries->groupBy('student_id');
|
|
// Filter out student groups with only one entry in the event
|
|
$grouped = $grouped->filter(fn ($s) => $s->count() > 1);
|
|
$doubler_array = [];
|
|
foreach ($grouped as $student_id => $entries) {
|
|
$doubler_array[$student_id] = [
|
|
'student' => $entries[0]->student,
|
|
'entries' => $entries,
|
|
];
|
|
}
|
|
|
|
return $doubler_array;
|
|
}
|
|
}
|