builder(); $builder->select('score') ->where('student_id', $studentId) ->where('semester', $semester) ->where('school_year', $schoolYear); // Execute the query and get the results $query = $builder->get(); $results = $query->getResultArray(); // If there are no scores, return 0 if (empty($results)) { return 0.0; } // Calculate the average score $totalScore = 0; $scoreCount = count($results); foreach ($results as $result) { $totalScore += $result['score']; } return $totalScore / $scoreCount; } }