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

jrdavidson's avatar

Deep Relationship Count

I have a deep count that I'm need to retrieve and would like some help figuring this out if anyone is willing to look at the current code and the database structure and assess what I have so far.

I know the first parameter of the withCount method if passed an array is the relationship but that's where I don't understand I'm not wanting it to be a count of the sections its deeper than that.

Any ideas/suggestions?

Course::query()
    ->whereHas('sections', function (Builder $query) {
        $query->whereHas('semesters', function ($query) {
             $query->where('semester_id', $this->semester->id);
        });
    })
    ->withCount(['testing' => function (Builder $query) {
        $query->whereHas('sections', function (Builder $query) {
            $query->whereHas('semesters', function ($query) {
                $query->where('semester_id', $this->semester->id);
            })->whereHas('students');
        });
    }])
Course
- id
- name
...

CourseSection
- id
- course_id
...

CourseSectionSemester
- id
- course_section_id
- semester_Id
...

CoureSectionSemesterStudent
- id
- course_section_semester_id
- student_Id
...

Course

public function sections()
{
    return $this->hasMany(CourseSection::class);
}

CourseSection

public function semesters()
{
    return $this->belongsToMany(Semester::class);
}

Semester

public function courseSections()
{
        return $this->belongsToMany(CourseSection::class)->using(CourseSectionSemester::class)->withTimestamps();
}

CourseSectionSemester

public function students()
{
    return $this->belongsToMany(User::class, 'course_section_semester_student', 'section_semester_id', 'student_id')->withTimestamps();
}
0 likes
4 replies
MichalOravec's avatar

Your code could look like this

$courses = Course::withCount(['sections' => function ($query) {
    $query->whereHas('semesters', function ($query) {
        $query->where('semester_id', $this->semester->id)->has('students');
    });
}])->whereHas('sections.semesters', function ($query) {
    $query->where('semester_id', $this->semester->id);
});

But I don't understand to this sentence

I'm not wanting it to be a count of the sections its deeper than that.

jrdavidson's avatar

@michaloravec What I was trying to convey is that I'm wanting the student count for each course in a course section during a semester.

That's a lot to take in. The suggested change wouldn't work because I don't have a student relationship on the semester model. I will provide update my original post with model relationships.

jrdavidson's avatar

@michaloravec I'm starting to wonder if any of my relationships aren't right because I can't figure out how to access the students.

jrdavidson's avatar

I've tried figuring this out this morning and haven't gotten any further. Has anyone used the withCount and had to go many levels deep?

Please or to participate in this conversation.