Compare commits

..

2 Commits

Author SHA1 Message Date
dredgy 4366c8f6ea Add countries and regions 2026-04-03 14:00:57 +10:00
dredgy d0191dbabd Add countries and regions 2026-04-03 13:57:25 +10:00
4 changed files with 165 additions and 0 deletions
+25
View File
@@ -0,0 +1,25 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Continent extends Model
{
protected $fillable = [
'code',
'name',
'internal_name',
];
public function countries(): HasMany
{
return $this->hasMany(Country::class);
}
public function regions(): HasMany
{
return $this->hasMany(Region::class);
}
}
@@ -0,0 +1,40 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('continents', function (Blueprint $table) {
$table->id();
$table->string('code', 2)->unique();
$table->string('name');
$table->string('internal_name');
});
DB::table('continents')->insert($this->continents());
}
public function down(): void
{
Schema::dropIfExists('continents');
}
private function continents(): array
{
return [
['code' => 'AF', 'name' => 'Africa', 'internal_name' => 'africa', ],
['code' => 'AN', 'name' => 'Antarctica', 'internal_name' => 'antarctica', ],
['code' => 'AS', 'name' => 'Asia', 'internal_name' => 'asia', ],
['code' => 'EU', 'name' => 'Europe', 'internal_name' => 'europe', ],
['code' => 'NA', 'name' => 'North America', 'internal_name' => 'north_america', ],
['code' => 'OC', 'name' => 'Oceania', 'internal_name' => 'oceania', ],
['code' => 'SA', 'name' => 'South America', 'internal_name' => 'south_america',],
];
}
};
@@ -0,0 +1,49 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('countries', function (Blueprint $table) {
$table->foreignId('continent_id')->nullable()->constrained('continents');
});
// Resolve continent codes already in the table
$continentMap = DB::table('continents')->pluck('id', 'code')->all();
foreach ($continentMap as $code => $id) {
DB::table('countries')
->where('continent', $code)
->update(['continent_id' => $id]);
}
Schema::table('countries', function (Blueprint $table) {
$table->foreignId('continent_id')->nullable(false)->change();
$table->dropColumn('continent');
});
}
public function down(): void
{
Schema::table('countries', function (Blueprint $table) {
$table->string('continent', 2);
});
$continentMap = DB::table('continents')->pluck('code', 'id')->all();
foreach ($continentMap as $id => $code) {
DB::table('countries')
->where('continent_id', $id)
->update(['continent' => $code]);
}
Schema::table('countries', function (Blueprint $table) {
$table->dropConstrainedForeignId('continent_id');
});
}
};
@@ -0,0 +1,51 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
// Add the FK column first, nullable so we can populate it before constraining
Schema::table('regions', function (Blueprint $table) {
$table->foreignId('continent_id')->nullable()->constrained('continents');
});
// Resolve continent codes already in the table
$continentMap = DB::table('continents')->pluck('id', 'code')->all();
foreach ($continentMap as $code => $id) {
DB::table('regions')
->where('continent', $code)
->update(['continent_id' => $id]);
}
// Now make it non-nullable and drop the old string column
Schema::table('regions', function (Blueprint $table) {
$table->foreignId('continent_id')->nullable(false)->change();
$table->dropColumn('continent');
});
}
public function down(): void
{
Schema::table('regions', function (Blueprint $table) {
$table->string('continent', 2);
});
$continentMap = DB::table('continents')->pluck('code', 'id')->all();
foreach ($continentMap as $id => $code) {
DB::table('regions')
->where('continent_id', $id)
->update(['continent' => $code]);
}
Schema::table('regions', function (Blueprint $table) {
$table->dropConstrainedForeignId('continent_id');
});
}
};