@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..