Skip to content

Commit 668fb19

Browse files
committed
delete hasmany child on parent delete
1 parent c132222 commit 668fb19

File tree

7 files changed

+80
-2
lines changed

7 files changed

+80
-2
lines changed

app/Models/Dipa.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Models;
44

5+
use App\Models\KamusAnggaran;
6+
use App\Models\MataAnggaran;
57
use Illuminate\Database\Eloquent\Factories\HasFactory;
68
use Illuminate\Database\Eloquent\Model;
79
use Illuminate\Database\Eloquent\Relations\HasMany;
@@ -45,8 +47,18 @@ public static function cacheEntities(): array
4547
protected static function booted(): void
4648
{
4749
static::deleting(function (Dipa $dipa) {
48-
MataAnggaran::where('dipa_id', $dipa->id)->delete();
49-
KamusAnggaran::where('dipa_id', $dipa->id)->delete();
50+
$kamusAnggaranIds = KamusAnggaran::where('dipa_id', $dipa->id)->pluck('id');
51+
KamusAnggaran::cache()->disable();
52+
KamusAnggaran::destroy($kamusAnggaranIds);
53+
KamusAnggaran::cache()->enable();
54+
KamusAnggaran::cache()->update('all');
55+
$mataAnggaranIds = MataAnggaran::where('dipa_id', $dipa->id)->pluck('id');
56+
MataAnggaran::cache()->disable();
57+
MataAnggaran::destroy($mataAnggaranIds);
58+
MataAnggaran::cache()->enable();
59+
MataAnggaran::cache()->update('all');
5060
});
5161
}
62+
63+
5264
}

app/Models/HargaSatuan.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ protected static function booted(): void
3838
{
3939
static::deleting(function (HargaSatuan $harga_satuan) {
4040
$jenisKontrakIds = JenisKontrak::where('harga_satuan_id', $harga_satuan->id)->pluck('id');
41+
JenisKontrak::cache()->disable();
4142
JenisKontrak::destroy($jenisKontrakIds);
43+
JenisKontrak::cache()->enable();
44+
JenisKontrak::cache()->update('all');
4245
});
4346
}
4447
}

app/Models/HonorSurvei.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Models;
44

55
use App\Helpers\Helper;
6+
use App\Models\DaftarHonor;
67
use Illuminate\Database\Eloquent\Factories\HasFactory;
78
use Illuminate\Database\Eloquent\Model;
89
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@@ -123,6 +124,11 @@ protected static function booted(): void
123124
});
124125
static::deleting(function (HonorSurvei $honor) {
125126
NaskahKeluar::destroy([$honor->sk_naskah_keluar_id, $honor->st_naskah_keluar_id]);
127+
$daftarHonorIds = DaftarHonor::where('honor_survei_id', $honor->id)->pluck('id');
128+
DaftarHonor::cache()->disable();
129+
DaftarHonor::destroy($daftarHonorIds);
130+
DaftarHonor::cache()->enable();
131+
DaftarHonor::cache()->update('all');
126132
});
127133
}
128134
}

app/Models/KerangkaAcuan.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Models;
44

55
use App\Helpers\Helper;
6+
use App\Models\AnggaranKerangkaAcuan;
67
use Illuminate\Database\Eloquent\Factories\HasFactory;
78
use Illuminate\Database\Eloquent\Model;
89
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@@ -85,6 +86,12 @@ protected static function booted(): void
8586
static::deleting(function (KerangkaAcuan $kak) {
8687
NaskahKeluar::destroy($kak->naskah_keluar_id);
8788
HonorSurvei::destroy($kak->id);
89+
$anggaranKerangkaAcuanIds = AnggaranKerangkaAcuan::where('kerangka_acuan_id', $kak->id)->pluck('id');
90+
AnggaranKerangkaAcuan::destroy($anggaranKerangkaAcuanIds);
91+
$arsipDokumenIds = ArsipDokumen::where('kerangka_acuan_id', $kak->id)->pluck('id');
92+
ArsipDokumen::destroy($arsipDokumenIds);
93+
$spesifikasiKerangkaAcuanIds = SpesifikasiKerangkaAcuan::where('kerangka_acuan_id', $kak->id)->pluck('id');
94+
SpesifikasiKerangkaAcuan::destroy($spesifikasiKerangkaAcuanIds);
8895
});
8996
static::created(function (KerangkaAcuan $kak) {
9097
ArsipDokumen::create(['slug' => 'Kerangka Acuan Kerja', 'kerangka_acuan_id' => $kak->id]);

app/Models/KodeNaskah.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Models;
44

5+
use App\Models\JenisNaskah;
56
use Illuminate\Database\Eloquent\Factories\HasFactory;
67
use Illuminate\Database\Eloquent\Model;
78
use Illuminate\Database\Eloquent\Relations\HasMany;
@@ -26,4 +27,15 @@ public static function cacheEntities(): array
2627
}),
2728
];
2829
}
30+
31+
protected static function booted(): void
32+
{
33+
static::deleting(function (KodeNaskah $kode_naskah) {
34+
$jenisNaskahIds = JenisNaskah::where('kode_naskah_id', $kode_naskah->id)->pluck('id');
35+
JenisNaskah::cache()->disable();
36+
JenisNaskah::destroy($jenisNaskahIds);
37+
JenisNaskah::cache()->enable();
38+
JenisNaskah::cache()->update('all');
39+
});
40+
}
2941
}

app/Models/TataNaskah.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace App\Models;
44

5+
use App\Models\DerajatNaskah;
6+
use App\Models\KodeArsip;
7+
use App\Models\KodeNaskah;
58
use Illuminate\Database\Eloquent\Factories\HasFactory;
69
use Illuminate\Database\Eloquent\Model;
710
use Illuminate\Database\Eloquent\Relations\HasMany;
@@ -43,4 +46,25 @@ public static function cacheEntities(): array
4346
}),
4447
];
4548
}
49+
50+
protected static function booted(): void
51+
{
52+
static::deleting(function (TataNaskah $tata_naskah) {
53+
$kodeArsipIds = KodeArsip::where('tata_naskah_id', $tata_naskah->id)->pluck('id');
54+
KodeArsip::cache()->disable();
55+
KodeArsip::destroy($kodeArsipIds);
56+
KodeArsip::cache()->enable();
57+
KodeArsip::cache()->update('all');
58+
$derajatnaskahIds = DerajatNaskah::where('tata_naskah_id', $tata_naskah->id)->pluck('id');
59+
DerajatNaskah::cache()->disable();
60+
DerajatNaskah::destroy($derajatnaskahIds);
61+
DerajatNaskah::cache()->enable();
62+
DerajatNaskah::cache()->update('all');
63+
$kodenaskahIds = KodeNaskah::where('tata_naskah_id', $tata_naskah->id)->pluck('id');
64+
KodeNaskah::cache()->disable();
65+
KodeNaskah::destroy($kodenaskahIds);
66+
KodeNaskah::cache()->enable();
67+
KodeNaskah::cache()->update('all');
68+
});
69+
}
4670
}

app/Models/User.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Models;
44

5+
use App\Models\DataPegawai;
6+
use App\Models\Pengelola;
57
use Illuminate\Database\Eloquent\Factories\HasFactory;
68
use Illuminate\Database\Eloquent\Relations\BelongsTo;
79
use Illuminate\Database\Eloquent\Relations\HasMany;
@@ -87,5 +89,17 @@ protected static function booted(): void
8789
static::creating(function (User $user) {
8890
$user->avatar = $user->avatar ?? 'G99ElrTEgEDRG4blE3m1xxMmFcfB0VVeLio0L3H6.jpg';
8991
});
92+
static::deleting(function (User $user) {
93+
$pengelolaIds = Pengelola::where('user_id', $user->id)->pluck('id');
94+
Pengelola::cache()->disable();
95+
Pengelola::destroy($pengelolaIds);
96+
Pengelola::cache()->enable();
97+
Pengelola::cache()->update('all');
98+
$dataPegawaiIds = DataPegawai::where('user_id', $user->id)->pluck('id');
99+
DataPegawai::cache()->disable();
100+
DataPegawai::destroy($dataPegawaiIds);
101+
DataPegawai::cache()->enable();
102+
DataPegawai::cache()->update('all');
103+
});
90104
}
91105
}

0 commit comments

Comments
 (0)