소스 검색

feat(student): trigger student status recalculation when contracts are saved or deleted

alvesantos 1 주 전
부모
커밋
1c2125a7c5
1개의 변경된 파일14개의 추가작업 그리고 0개의 파일을 삭제
  1. 14 0
      app/Models/StudentContract.php

+ 14 - 0
app/Models/StudentContract.php

@@ -136,6 +136,20 @@ public function scopeLatestVersion(Builder $query): Builder
         );
     }
 
+    protected static function booted(): void
+    {
+        $updateStudentStatus = function (StudentContract $contract): void {
+            if ($contract->student_id) {
+                // Use withoutGlobalScopes or just normal relation if no scopes affect it
+                $contract->student()->first()?->updateStatus();
+            }
+        };
+
+        static::saved($updateStudentStatus);
+        static::deleted($updateStudentStatus);
+        static::restored($updateStudentStatus);
+    }
+
     public function derivedFrom(): BelongsTo
     {
         return $this->belongsTo(self::class, 'derived_from_contract_id');