@foreach($subjects as $sub) @endforeach @php $students = $students->map(function ($s) use ($marks, $tex, $subjects, $exam_id) { $s->computed_total = $marks ->where('student_id', $s->user_id) ->where('exam_id', $exam_id) ->sum($tex); return $s; })->sortByDesc('computed_total')->values(); $prev_total = null; $rank = 0; $position = 0; @endphp @foreach($students as $index => $s) @php if ($s->computed_total !== $prev_total) { $rank = ++$position; $prev_total = $s->computed_total; } $subject_count = $subjects->count(); $student_average = $subject_count > 0 ? round($s->computed_total / $subject_count, 2) : '-'; @endphp @foreach($subjects as $sub) @endforeach @endforeach
# NAMES_OF_STUDENTS_IN_CLASS{{ strtoupper($sub->slug ?: $sub->name) }}Total Average Position
{{ $rank }} {{ $s->user->name }}{{ $marks->where('student_id', $s->user_id)->where('subject_id', $sub->id)->first()->$tex ?? '-' }}{{ $s->computed_total ?: '-' }} {{ $student_average ?: '-' }} {{ \App\Helpers\Mk::getSuffix($rank) }}