Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AbdulBazith's avatar

Sorting the student mark which is calculated in the laravel blade file using php

guys i am working with a project student online examination

i have two tables to store the student answer. first table stores student name and assessment_id and the second table stores the answers of the particular student

StudentExamMain - >stores student_id, assessment_id, exam_date

StudentExamSub -> stores the studentexammain_id, stud_answer

my controller query

 $studentexams= StudentExamMain::with('studentexamsubdetails')->where('assessment_id', $id)->get();

iam calculating the student mark in the blade file like below


@foreach ($studentexams as $studentexam) // iterating students one by one

@php $totalmark=0;@endphp  //declared a variable to calcuate the total mark

@foreach($studentexam->studentexamsubdetails as $answers) // iterating the single student with his answers

@php $range=0; @endphp  // declared a varibe to calculate percentage

@if($answers->stud_answer==$question->questiondetails->correct_answer) // checking the if condition if answer from question table and answer of the student matches add 1 point. 

@php $totalmark=$totalmark + 1; @endphp

@endif

@endforeach

@php $range=($totalmark/30)*100 @endphp  // calculating the percentage for that single student.

@endphp

Everything works fine. but i need to sort the data in descending order with mark. those who got high mark should be displayed first.

currently my output coding like this

<div class="progressBar">

    <h4>{{ $studentexam->user->name  }} ({{ $studentexam->user->username }}) -
        {{ number_format($range,2) }} % </h4>

</div>


// my output screen looks like this for the above coding

Abdul (A001) - 62 %

Bazith (B002) - 80 %

Imran (I001) - 52 %

Muki (M002) - 90 %

Rahman (R002) - 96 %

i got everything i expected. but i need to sort the students based on the mark. top ark should come first in descending order.

kindly some one help please..

0 likes
0 replies

Please or to participate in this conversation.