'required|string|max_length[9]', ]; /** * Insert or update a semester score record */ // In SemesterScoreModel.php public function upsert(array $data): bool { $data = $this->withDefaultSchoolYear($data); // First check if record exists $existing = $this->where([ 'student_id' => $data['student_id'], 'semester' => $data['semester'], 'school_year' => $data['school_year'] ])->first(); // If exists - UPDATE if ($existing) { return $this->update($existing['id'], $data); } // If new - INSERT else { return $this->insert($data) !== false; } } }