'integer', 'from_class_section_id' => 'integer', 'to_class_id' => 'integer', 'to_class_section_id' => 'integer', 'updated_by' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', ]; /* ============================================================ * Relationships (optional) * ============================================================ */ public function student(): BelongsTo { return $this->belongsTo(Student::class, 'student_id'); } public function fromClassSection(): BelongsTo { return $this->belongsTo(ClassSection::class, 'from_class_section_id'); } public function toClassSection(): BelongsTo { return $this->belongsTo(ClassSection::class, 'to_class_section_id'); } public function toClass(): BelongsTo { return $this->belongsTo(SchoolClass::class, 'to_class_id'); // rename model if yours is ClassModel/ClassRoom/etc. } /* ============================================================ * Scopes (reusable filters) * ============================================================ */ public function scopeForStudent(Builder $q, int $studentId): Builder { return $q->where('student_id', $studentId); } public function scopeForSchoolYearTo(Builder $q, string $schoolYearTo): Builder { return $q->where('school_year_to', $schoolYearTo); } public function scopeStatus(Builder $q, string $status): Builder { return $q->where('status', $status); } /* ============================================================ * Upsert (Laravel style) * ============================================================ */ /** * True upsert by (student_id, school_year_to). * * Returns the saved model, or null if required keys missing. */ public static function upsertQueue(array $data): ?self { $studentId = isset($data['student_id']) ? (int) $data['student_id'] : null; $yearTo = isset($data['school_year_to']) ? (string) $data['school_year_to'] : null; if (! $studentId || ! $yearTo) { return null; } // Only update columns that are allowed. $payload = array_intersect_key($data, array_flip((new static)->getFillable())); return static::updateOrCreate( ['student_id' => $studentId, 'school_year_to' => $yearTo], $payload ); } /** * Optional: keep the same legacy signature (bool result). */ public static function upsertBool(array $data): bool { return (bool) static::upsertQueue($data); } }