Files
database-kesiswaan/database/migrations/2026_04_11_160800_create_regions_tables.php
T
2026-04-12 06:24:30 +00:00

76 lines
3.0 KiB
PHP

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('provinces', function (Blueprint $table) {
$table->string('id', 20)->primary();
$table->string('name', 100);
$table->timestamps();
});
Schema::create('regencies', function (Blueprint $table) {
$table->string('id', 20)->primary();
$table->string('province_id', 20);
$table->string('name', 100);
$table->timestamps();
$table->foreign('province_id')->references('id')->on('provinces')->cascadeOnUpdate()->cascadeOnDelete();
$table->index('province_id');
});
Schema::create('districts', function (Blueprint $table) {
$table->string('id', 20)->primary();
$table->string('regency_id', 20);
$table->string('name', 100);
$table->timestamps();
$table->foreign('regency_id')->references('id')->on('regencies')->cascadeOnUpdate()->cascadeOnDelete();
$table->index('regency_id');
});
Schema::create('villages', function (Blueprint $table) {
$table->string('id', 20)->primary();
$table->string('district_id', 20);
$table->string('name', 100);
$table->timestamps();
$table->foreign('district_id')->references('id')->on('districts')->cascadeOnUpdate()->cascadeOnDelete();
$table->index('district_id');
});
Schema::table('students', function (Blueprint $table) {
$table->string('province_id', 20)->nullable()->after('address');
$table->string('regency_id', 20)->nullable()->after('province_id');
$table->string('district_id', 20)->nullable()->after('regency_id');
$table->string('village_id', 20)->nullable()->after('district_id');
$table->foreign('province_id')->references('id')->on('provinces')->nullOnDelete()->cascadeOnUpdate();
$table->foreign('regency_id')->references('id')->on('regencies')->nullOnDelete()->cascadeOnUpdate();
$table->foreign('district_id')->references('id')->on('districts')->nullOnDelete()->cascadeOnUpdate();
$table->foreign('village_id')->references('id')->on('villages')->nullOnDelete()->cascadeOnUpdate();
});
}
public function down(): void
{
Schema::table('students', function (Blueprint $table) {
$table->dropForeign(['province_id']);
$table->dropForeign(['regency_id']);
$table->dropForeign(['district_id']);
$table->dropForeign(['village_id']);
$table->dropColumn(['province_id', 'regency_id', 'district_id', 'village_id']);
});
Schema::dropIfExists('villages');
Schema::dropIfExists('districts');
Schema::dropIfExists('regencies');
Schema::dropIfExists('provinces');
}
};