db->table('homework'); $builder->selectAvg('score', 'average_score') ->where('student_id', $studentId) ->where('semester', $semester) ->where('school_year', $schoolYear) ->where('score IS NOT NULL'); // Only include records with actual scores // Execute the query $result = $builder->get()->getRow(); // Return the average or 0.0 if no results return $result ? (float)$result->average_score : 0.0; } }