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

AbdulBazith's avatar

Where and group by condition in child table when eloquent from parent table

Guys iam working with a project School Management System.

i have tables like below

Table: ExaminationDetails

id  year_id     exam_code       exam_name
1   1           EX01            QuarterlyExam
2   1           Ex02            Halfyearly Exam

Table: SubjectDetails

id  year_id     subject_code        subject_name
1   1           S01             English
2   1           S02             Maths

Table: MainMark

id  year_id class_id        section_id  exam_id
1   1       1           1           1
2   1       1           1           2
// here i have added a class 1= V std section 1= A section exam id 1=QuarterlyExam

Table: SubMark

id  year_id exam_date   student_id  subject_id  mark
1   1       25-01-20    1           1           80
2   1       25-01-20    2           1           90
3   1       25-01-20    3           1           98
4   1       25-01-20    4           1           70
5   1       25-01-20    5           1           60

// Here i have added  5 students of same class V std A section for same subject  English marks

Now brought the results based on student id with specific examination everything works fine.

Howi wrote the query is

//The below query fetched the record from mainmark table. only one exam can be given so i gave first(). so only one record will be fetched.

 $students = StudentMainMark::where(function ($query) use ($request) {

 if (!empty($request->class_id)) {

 $query->where('class_id', $request->class_id);
 }
 if (!empty($request->section_id)) {

 $query->where('section_id', $request->section_id);
 }
 if (!empty($request->exam_id)) {

 $query->where('exam_id', $request->exam_id);
 }

 })->first();

//Here after fetching from main mark table i gave that id to submark table to where condition to fetch the subject. this is for listing the subjects in table header <th>

 $headers =StudentSubMark::where('main_mark_id', $students->id)->orderBy('subject_id')->distinct('subject_id')->get();

// this  groups the data student_name groupby. this is for tbody in blade file.

        $groups = StudentSubMark::where('main_mark_id', $students->id)->orderBy('subject_id')->get()->groupBy('user.user_name');

This is my blade file

// this displays the subject with unique name.
 <thead>
        <tr class="bhg">
            <th>S.No</th>
            <th>Name</th>
            @foreach($headers->unique('subject_id') as $header)
            <th>{{  $header->subject->sub_name }}</th>
            @endforeach
       
        </tr>
    </thead>

for the above code my output will look like this

Sno     Name    English Maths   Social  



//Next this is tbody that displays student name and marks

  @foreach($groups as $student => $results)
<tr>
            <td> {{ $loop->iteration }} </td>
            <td>{{$student}}</td>
            @foreach($results as $result)
          <td> {{$result->mark}}</td>
              @endforeach
         
        </tr>
        @endforeach

for the above blade file the output will be

Sno     Name        English maths   social
1       AA          20      30      40  
2       BB          30      40      60

My output screen is : https://imgur.com/NWBtdd0

Everythig is ok. but i have few problems report must change based on user selection

the above code is if user selects class, section and exam name

but what i expect is if user selects only class and section (subject. exam, student these 3 not selected. these three are options). so i planned to to have if condtions in controller and based on that it redirects to different blade file.

if only class and section is selected then my output must look like

Class: V std    Section: A

Examination: Quarterly

Student     english maths   social 
abdul       80      30      20
bazith      20      70      60

Examination: Halfyearly

student     English maths   social
abdul       60      70      80
bazith      70      60      90

/ / this shows all the examination of a single class with all student.

i tried this code

 $students = StudentMainMark::where(function ($query) use ($request) {

 if (!empty($request->class_id)) {

 $query->where('class_id', $request->class_id);
 }
 if (!empty($request->section_id)) {

 $query->where('section_id', $request->section_id);
 }

 })->get();

 $students->load('studentsubmark');

But i dont know how to use groupby in the child table that is studentsubmark.

but the above query worked till tag. but i cant print tbody portion because i need to groupby student wise see

  @foreach($students as $ss)
  <p class="fee-bold">Exam:{{$ss->examm->exam_name}} </p>

  <thead>
      <tr class="bhg">
          <th>S.No</th>
          <th>Name</th>
          @foreach($ss->studentsubmark->unique('subject_id') as $header)
          <th>{{  $header->subject->sub_name }}</th>
          @endforeach
          <th>Total</th>
      </tr>
  </thead>

//This above code prints the head portion but the body portion i cant groupby kindly some one help please

My models

//Mainmark model
 public function studentsubmark()
    {
        return $this->hasmany('App\StudentSubMark', 'main_mark_id');
    }

//submark model
 public function studentmainmark()
    {
        return $this->belongsTo('App\StudentMainMark', 'main_mark_id');
    }
  public function examm()
    {
        return $this->belongsTo('App\ExaminationInfo', 'exam_id');
    }

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

Refer this link for more information: https://laracasts.com/discuss/channels/eloquent/reports-in-view-must-change-based-on-user-selectiong-os-name-or-class-or-subject-or-examination

Kindly some one help please

0 likes
2 replies
a4ashraf's avatar

Hello @abdulbazith

the question you asked, "But i dont know how to use groupby in the child table that is studentsubmark."

this following code will help in your

try this


    $students = StudentMainMark::where(function ($query) use ($request) {

        if (!empty($request->class_id)) {
            $query->where('class_id', $request->class_id);
        }

        if (!empty($request->section_id)) {
            $query->where('section_id', $request->section_id);
        }

    })->get();

    $students->load(['studentsubmark' => function ($query) {
        $query->groupBy('your_group_by_field');
    }]);

AbdulBazith's avatar

@a4ashraf thank you for your response. and very sorry for my late response. actually i stuck into another issue.

i will try your reply and form you

Please or to participate in this conversation.