selectRaw('sc.class_section_id, COUNT(DISTINCT sc.student_id) AS student_count') ->join('students as s', 's.id', '=', 'sc.student_id') ->where('s.is_active', 1) ->where('sc.school_year', $schoolYear); if ($limitToSemester && $semester !== '') { $query->where('sc.semester', $semester); } return $query->groupBy('sc.class_section_id')->get()->toArray(); } public function getStudentCountForSection(string $schoolYear, string $semester, bool $limitToSemester, string $classSectionId): int { $query = DB::table('student_class as sc') ->selectRaw('COUNT(DISTINCT sc.student_id) AS cnt') ->join('students as s', 's.id', '=', 'sc.student_id') ->where('s.is_active', 1) ->where('sc.class_section_id', $classSectionId) ->where('sc.school_year', $schoolYear); if ($limitToSemester && $semester !== '') { $query->where('sc.semester', $semester); } $row = $query->first(); return (int) ($row->cnt ?? 0); } }