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();
}