jay_gorio
3 years ago

Nested eager loading with constraint not working

Posted 3 years ago by jay_gorio

I am having hard time using nested with with constraint. Can someone help me to group subjects based on the what semester it will be offered.

MyController

    public function show($id)
    {
        $student = Student::with(['course', 'course.curriculum', 'course.curriculum.subjects' => function($query){
            $query->groupBy('sem_offered');
        }])->findOrFail($id);

        return view('students.show', compact('student'));
    }

show.index

@foreach($student->course->curriculum->subjects['first first-sem'] as $subject)
<tr>
    <td>{{ $subject->subject_code }}</td>
    <td>{{ $subject->subject_description }}</td>
    <td>{{ $subject->units }}</td>
    <td></td>
    <td>98</td>
</tr>
@endforeach

Please sign in or create an account to participate in this conversation.