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

AbdulBazith's avatar

Display records from row wise to column wise in blade file laravel

Guys iam working with a project.

i have a table Student_mark with columns the below is my table structure

id  exam        class   student     subject     marks
1   monthly     V       AAA         english     60
2   monthly     V       BBB         Engish      70
3   monthly     V       CCC         English     90
4   monthly     V       AAA         Maths       89
5   monthly     V       BBB         Maths       70
6   monthly     V       CCC         Maths       45

iam expecting the output like below in my blade file

Exam:Monthly        Class: V

Sno     Student     English     Maths
1       AAA         60          89      
2       BBB         70          70
3       CCC         90          45

How can i achieve this?

actually i asked this in my previous thread also. but i made this thread very simple. my problem is data are repeating. the names and subjects are repeating so what can i do?? Kindly someone suggest any idea

the brief details is here in this thread:

https://laracasts.com/discuss/channels/laravel/error-in-student-mark-db-structure-insert-and-view

But if you answer for this i can compare it with my tables and make it.

0 likes
9 replies
vandan's avatar

@abdulbazith ok

for example try this

$users = DB::table('users')
        ->select('id','exam', 'class','student','subject','marks')
        ->groupBy('id')
        ->get();

or else

DB::table('table_name')->distinct()->get(['column_name']);

Eloquent:

MyModel::distinct()->get(['column_name']);
AbdulBazith's avatar

@van-india thank you for your response

i tried like this

$query= StudentSubMark::distinct()->get(['student_id','subject_id','mark']);

in my blade

<thead>
    <tr class="unread bggrn">
        <th>S.No</th>
        <th>Name</th>

        @foreach($query as $sub)

        <th>{{  $sub->subject->sub_name }}</th>

        @endforeach

    </tr>
</thead>
<tbody class="capital">

    @foreach($query as $sub)

    <tr class="profile-table">
        <td> {{ $loop->iteration }} </td>

        <td> <a href="">{{ $sub->user->user_name }} </a> </td>
        <td> {{  $sub->mark }} </td>
        <td>{{ $sub->student_remarks }}</td>
    </tr>

    @endforeach

</tbody>

but the output looks like this

S.No        Name        Tamil   English
1           Muthu       30  
2           Muthu       60  

but currently what i have in my db is

id  exam        class   student     subject     marks
1   monthly     V       muth         tamil      30
2   monthly     V       muthu       Engish      60

whats the problem??i have entered muthu's tamil and english mark. both tamil and english mark are displayed in tamil column. i need it to be displayed in english.

Sinnbeck's avatar

You can group all results by the student

$groups = MyModel->get()->groupBy('student');

This will make a group of all results for each student. Be aware that you need to order the results properly to fit with the headers

@foreach($groups as $student => $results)
    <tr><td>{{$student}}</td>
    @foreach($results as $result)
         <td>{{$result->marks}}</td
    @endforeach
    </tr>
@endforeach
AbdulBazith's avatar

@sinnbeck thank yu sooo much for your response. it worked but having problem in table heading (headers ) as you said.

in header i need like these

StudentName     English     Maths       Science ....... (all these are in mark table as subject_id)

Kidnly help me for that tooo pleasee

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Something like this I would think

$headers = MyModel->distinct()->orderBy('subject')->get();
$groups = MyModel->orderBy('subject')->get()->groupBy('student');

//headers
@foreach($headers as $header)
    <th>{{  $header->subject }}</th>
AbdulBazith's avatar

@sinnbeck once again thank you soo much. everything worked fine with one problem

what i did is,

in my controller

 $headers =StudentSubMark::distinct()->orderBy('subject_id')->get();     
        
$groups = StudentSubMark::orderBy('subject_id')->get()->groupBy('student_id');

actually distinct is not working. i dont know why.

In my blade file for my headers. unique() works fine for me for subject duplication problem if i remove this unique subjects are repeated.

<thead>
    <tr >
        <th>S.No</th>
        <th>Name</th>

        @foreach($headers->unique('subject_id') as $header)
        <th>{{  $header->subject->sub_name }}</th>
        @endforeach

        <th>Total</th>
    </tr>
</thead>

and for my body portion

<tbody>

    @foreach($groups as $student => $results)
    @php
    $total = 0;
    @endphp
    <tr>
        <td> {{ $loop->iteration }} </td>
        <td>{{$student }}</td>
//Here i face a problem, $student has id of that student. but i need the student name. i explained it below




        @foreach($results as $result)

        @if($result->mark==0)
        <td><b>A</b></td>
        @else
        <td>{{$result->mark ?? 0}}</td>
        @endif

        @php
        $total += $result->mark;
        @endphp

        @endforeach

        <td><b>{{$total}}</b></td>
    </tr>
    @endforeach

</tbody>

$student has id of that student. but i need the student name. i coded like below

{{$student->user->user_name ?? 'problem' }}

the problem is printed instead the name of the student. if i remove the problem and code like below

{{$student->user->user_name }} this shows error Trying to get property 'user' of non-object

whats the problem???

my models are

My User model

public function studentsubmark()
     {
         return $this->hasmany('App\StudentSubMark', 'student_id');
     }


My SubMark Model

 public function user()
    {
        return $this->belongsTo('App\User', 'student_id');
    }


when i dd($groups)

Collection {#552 ▼
  #items: array:8 [▼
    321 => Collection {#535 ▶}
    319 => Collection {#526 ▶}
    320 => Collection {#536 ▶}
    318 => Collection {#544 ▶}
    313 => Collection {#545 ▶}
    310 => Collection {#553 ▶}
    311 => Collection {#549 ▶}
    312 => Collection {#551 ▶}
  ]
}

all the numbers are student id

Kindly answer this please. and also suggest a table structure if possible.

i have to enter class, section, exam, subect, student, mark.

i have divied it onto two tables whether that is right or not that is in this thread

Link:

https://laracasts.com/discuss/channels/laravel/error-in-student-mark-db-structure-insert-and-view

if possible answer the link thread else kindly answer to this thread about the student name..

bhamra's avatar

Sir i am trying to do almost same thing but unable to get desired results

i want to get result like this in json format

data:
0:
Exam_Name : "first term"
marks : "English : 75, Math : 80, Science : 70"
1:
Exam_Name : "second term"
marks : "English : 72, Math : 82, Science : 86"
There are two tables Exam & exam results
Exam has id, name, class_id columns
id         name           class_id
1       first term             1
2       second term            1

Exam_Results has exam_id, student_id  subject, marks columns
id       exam_id     student_id        subject      marks
1         1            1                english       75
2         1            1                 math         80
3         1            1                science       70
4         2            1                english       72
5         2            1                 math         82
6         2            1                science       86

Here is my controller file

$results =ExamResults::join('exam','exam_results.exam_id', '=', 'exam.id')
        ->select('exam.name','exam_results.*')
        ->where('student_id',$student->id)->get();

$data   = [];

foreach($results as $row){
    $data[] = [
        'exam_name'          =>$row->name,
        'marks'         =>$row->subject." : ".$row->marks,
        ];
}
return response()->json(['data' => $data,]);

with this i m getting result like below

data:
0:
exam_name: "first term"
marks: "english : "75"
1:
exam_name: "first term"
marks: "math : "80"
2:
exam_name: "first term"
marks: "science : "70"
3:
exam_name: "second term"
marks: "english : "72"
4:
exam_name: "second term"
marks: "math : "82"
5:
exam_name: "second term"
marks: "science : "86"

Please help

Please or to participate in this conversation.