/** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $students = Student::all(); $marks = Mark::all(); foreach ($students as $student) { $marks_count = 0; $marks_value = 0; foreach ($marks as $mark) { if ($mark->student_id == $student->id) { $marks_count++; $marks_value += $mark->value_2; } } if ($marks_count != 0) { $student['mark_avg'] = $marks_value / $marks_count; } else { $student['mark_avg'] = 0; } } $students = $students->sortByDesc('mark_avg'); $students = $students->splice(0, 10); return view('students.index', ['students' => $students]); }