Skip to content

Commit e0159c3

Browse files
committed
Refactor DaftarKegiatan and DaftarReminder models to streamline deletion logic and update status handling for improved efficiency
1 parent ba55846 commit e0159c3

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

app/Models/DaftarKegiatan.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ protected static function booted(): void
5252
}
5353
});
5454
static::deleting(function (DaftarKegiatan $daftar) {
55-
$ids = DaftarReminder::where('daftar_kegiatan_id', $daftar->id)->get()->pluck('id');
56-
DaftarReminder::destroy($ids);
55+
DaftarReminder::where('daftar_kegiatan_id', $daftar->id)->delete();
5756
});
5857

5958
static::creating(function (DaftarKegiatan $daftar) {
@@ -73,7 +72,7 @@ protected static function booted(): void
7372
]);
7473
$reminder->save();
7574
}
76-
DaftarKegiatan::where('id', $daftar->id)
75+
$daftar
7776
->whereHas('daftarReminder', function ($query) {
7877
$query->where('status', '!=', 'sent');
7978
})

app/Models/DaftarReminder.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ protected static function booted(): void
3131
static::creating(function (DaftarReminder $daftar) {
3232
$daftar->status = 'on progress';
3333
});
34+
static::deleting(function (DaftarReminder $daftar) {
35+
$daftar->daftarKegiatan->whereDoesntHave('daftarReminder', function ($query) use ($daftar) {
36+
$query->where('status', '!=', 'sent')->where('id', '!=', $daftar->id);
37+
})->update(['status' => 'sent']);
38+
});
3439

3540
}
3641

0 commit comments

Comments
 (0)