Fix relasi database peminjaman

This commit is contained in:
wardhatul1765 2026-04-30 07:00:13 +07:00
parent 01c985e0f6
commit 57fd820dfa
3 changed files with 32 additions and 24 deletions

View File

@ -120,7 +120,18 @@ public function store(Request $request)
if ($response->successful() && ($response->json('status') == true)) {
$waSuccess = true;
} else {
\Illuminate\Support\Facades\Log::warning("Fonnte Log: Gagal Terkirim", [
'target' => $targetNum,
'http_status' => $response->status(),
'body' => $response->body()
]);
}
} else {
\Illuminate\Support\Facades\Log::warning('Fonnte Log: Token / No HP kosong', [
'target' => $targetNum ?? 'kosong',
'token_set' => !empty($fonnteToken),
]);
}
} catch (\Exception $e) {
\Illuminate\Support\Facades\Log::error("Error WA Pengiriman: " . $e->getMessage());

View File

@ -96,7 +96,7 @@ public function store(Request $request)
// Dapatkan nomor handphone pendaftar & Fonnte Token
$targetNum = $peminjaman->anggota->no_hp ?? '';
$fonnteToken = env('vpzqxF2ZGgTGz9F5UbUS'); // Pastikan token fonnte dimasukkan di file .env Anda.
$fonnteToken = 'vpzqxF2ZGgTGz9F5UbUS'; // Token Fonnte
// Standarisasi Format Nomor ke awalan 62 (Kode Internasional Indonesia) standar yang dianjurkan Fonnte
if (!empty($targetNum)) {

View File

@ -12,33 +12,30 @@
*/
public function up(): void
{
Schema::table('peminjaman', function (Blueprint $table) {
// Drop old FKs carefully
try {
DB::statement('ALTER TABLE peminjaman DROP FOREIGN KEY peminjaman_ibfk_1');
} catch (\Exception $e) {
// Ignore if not exists
}
// 1. Hapus semua kemungkinan Foreign Key lama agar tidak bentrok
try { DB::statement('ALTER TABLE peminjaman DROP FOREIGN KEY peminjaman_ibfk_1'); } catch (\Exception $e) {}
try { DB::statement('ALTER TABLE peminjaman DROP FOREIGN KEY peminjaman_ibfk_2'); } catch (\Exception $e) {}
try { DB::statement('ALTER TABLE peminjaman DROP FOREIGN KEY fk_peminjaman_user'); } catch (\Exception $e) {}
try { DB::statement('ALTER TABLE peminjaman DROP FOREIGN KEY fk_peminjaman_buku'); } catch (\Exception $e) {}
try {
DB::statement('ALTER TABLE peminjaman DROP FOREIGN KEY peminjaman_ibfk_2');
} catch (\Exception $e) {
// Ignore if not exists
}
// Menghapus default foreign key dari Laravel
try { Schema::table('peminjaman', function (Blueprint $table) { $table->dropForeign(['id_user']); }); } catch (\Exception $e) {}
try { Schema::table('peminjaman', function (Blueprint $table) { $table->dropForeign(['id_buku']); }); } catch (\Exception $e) {}
// Change column types
// 2. Samakan Tipe Data (Ditaruh di LUAR Schema::table agar pasti dieksekusi duluan)
DB::statement('ALTER TABLE peminjaman MODIFY id_user BIGINT UNSIGNED NOT NULL');
DB::statement('ALTER TABLE peminjaman MODIFY id_buku BIGINT UNSIGNED NOT NULL');
DB::statement('ALTER TABLE peminjaman MODIFY id_buku INT NOT NULL');
// Add correct foreign keys
// 3. Buat Relasi Baru yang Benar
Schema::table('peminjaman', function (Blueprint $table) {
$table->foreign('id_user')
->references('id')
->on('users')
->onDelete('cascade');
$table->foreign('id_buku')
->references('id')
->on('bukus')
->references('id_buku')
->on('buku')
->onDelete('cascade');
});
}
@ -49,9 +46,9 @@ public function up(): void
public function down(): void
{
Schema::table('peminjaman', function (Blueprint $table) {
// Hapus kedua relasi jika migration di-rollback
$table->dropForeign(['id_user']);
// We cannot easily recreate the "wrong" FK without knowing exact wrong definition,
// but usually we don't need to revert a fix to a broken state.
$table->dropForeign(['id_buku']);
});
}
};