private function calcRates() { $candidates = []; $this->userNotRatedMovies->each(function ($movieA, $index) use(&$candidates) { $diffNote = []; $this->userRatedMovies->each(function ($note, $index) use($movieA, &$diffNote) { $diff = Mdiff::where('movieA_id', $movieA->_id)->first(); if (empty($diff)) { $diff = Mdiff::where('movieB_id', $movieA->_id)->where('movieA_id', $note->movie_id)->first(); } if (!empty($diff)) { array_push($diffNote, $diff->diff); } }); if (empty($diffNote)) { $note = 0; } else { $note = array_sum($diffNote) / count($diffNote); } array_push($candidates, ['note' => $note, 'movie' => $movieA]); }); return $this->orderCandidates($candidates); }