I have done one-to-many relationships but when I loop through it in my view only one subject displays even when I have more than one subjects.
This is what I have done in my controller
$broadsheet = Broadsheet::with(['subject', 'session', 'term', 'student', 'classarm'])
->where('session_id', $request->sessions)
->where('term_id', $request->terms)
->where('class_arm_id', $request->classarms)
->selectRaw('sum(total) as totals, count(subject_id) as countrow, student_id, subject_id, session_id, term_id, class_arm_id, total')
->groupBy('student_id')
->orderBy('totals', 'desc')
->get();
return view('broadsheet.index')->with('broadsheet', $broadsheet);
This is the index view
@extends('layouts.app')
@section('content')
{{--@foreach($broadsheet as $subjects)
@foreach ($subjects->subject as $sub)
<th> {{ strtoupper($sub->subject->subject_name) }}</th>
@endforeach
@endforeach--}}
<div class="table-responsive">
<table class="table table-striped table-sm">
<thead>
<tr>
@foreach($broadsheet as $subject)
<th> {{ strtoupper($subject->subject->subject_name) }}</th>
@endforeach
<th>TOTAL</th>
<th>AVERAGE</th>
<th>POSITION</th>
</tr>
</thead>
<tbody>
@foreach($broadsheet as $result)
<tr>
<td>{{ $result->total }}</td>
<td>{{ $result->totals }}</td>
<td>{{ number_format($result->totals/$result->countrow,2) }}</td>
<td>{{ $i++ }}</td>
<td>{{ $result->exams }}</td>
<td>{{ $result->total }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
@endsection
and this is my Broadsheet schema
class Broadsheet extends Model
{
use HasFactory;
protected $fillable = [
'student_id',
'subject_id',
'session_id',
'term_id',
'class_arm_id',
'subject_total',
];
public function student()
{
return $this->belongsTo(Student::class, 'student_id');
}
public function subject()
{
return $this->belongsTo(Subject::class, 'subject_id');
}
public function session()
{
return $this->belongsTo(Session::class, 'session_id');
}
public function term()
{
return $this->belongsTo(Term::class, 'term_id');
}
public function classarm()
{
return $this->belongsTo(ClassArm::class, 'class_arm_id');
}
}
I want to display students results like this Intended result
Please, I would really need assistance to get this working as any help would be greatly appreciated.I just started learning laravel recently. I don't really know where the issue is coming from