auditionCacheService = $scoringGuideCacheService; } public function getScoredEntries() { return Cache::remember($this->cacheKey, 10, function () { $entries = Entry::with(['scoreSheets'])->get(); return $entries->keyBy('id'); }); } public function getAuditionsWithStatus() { // Create an array with the number of scores for each entry $scoreCountByEntry = ScoreSheet::select('entry_id', DB::raw('count(*) as count')) ->groupBy('entry_id') ->get() ->pluck('count','entry_id'); // Retrieve auditions from the cache and load entry IDs $auditions = $this->auditionCacheService->getAuditions(); $auditions->load('entries'); // Eager load the count of related models $auditions->loadCount(['judges', 'entries']); // Iterate over the auditions and calculate the scored_entries_count return $auditions->map(function ($audition) use ($scoreCountByEntry) { $audition->scored_entries_count = $audition->entries->reduce(function ($carry, $entry) use ($audition, $scoreCountByEntry) { $entry->fully_scored = $audition->judges_count == $scoreCountByEntry[$entry->id]; return $carry + ($entry->fully_scored ? 1 : 0); }, 0); return $audition; }); } }