generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
529 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('barangays', function (Blueprint $table) { | ||
$table->string('code')->primary(); | ||
$table->string('old_code')->nullable(); | ||
$table->string('region_code'); | ||
$table->string('province_code'); | ||
$table->string('municipality_code'); | ||
$table->string('sub_municipality_code'); | ||
$table->string('barangay_code'); | ||
$table->string('name'); | ||
$table->string('old_name')->nullable(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('barangays'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('cities', function (Blueprint $table) { | ||
$table->string('code')->primary(); | ||
$table->string('old_code')->nullable(); | ||
$table->string('region_code'); | ||
$table->string('province_code'); | ||
$table->string('city_code'); | ||
$table->string('name'); | ||
$table->string('old_name')->nullable(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('cities'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('municipalities', function (Blueprint $table) { | ||
$table->string('code')->primary(); | ||
$table->string('old_code')->nullable(); | ||
$table->string('region_code'); | ||
$table->string('province_code'); | ||
$table->string('municipality_code'); | ||
$table->string('name'); | ||
$table->string('old_name')->nullable(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('municipalities'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('provinces', function (Blueprint $table) { | ||
$table->string('code')->primary(); | ||
$table->string('old_code')->nullable(); | ||
$table->string('region_code'); | ||
$table->string('province_code'); | ||
$table->string('name'); | ||
$table->string('old_name')->nullable(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('provinces'); | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('regions', function (Blueprint $table) { | ||
$table->string('code')->primary(); | ||
$table->string('old_code')->nullable(); | ||
$table->string('region_code'); | ||
$table->string('name'); | ||
$table->string('old_name')->nullable(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('regions'); | ||
} | ||
}; |
33 changes: 33 additions & 0 deletions
33
database/migrations/create_sub_municipalities_table.php.stub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('sub_municipalities', function (Blueprint $table) { | ||
$table->string('code')->primary(); | ||
$table->string('old_code')->nullable(); | ||
$table->string('region_code'); | ||
$table->string('province_code'); | ||
$table->string('municipality_code'); | ||
$table->string('sub_municipality_code'); | ||
$table->string('name'); | ||
$table->string('old_name')->nullable(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('sub_municipalities'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
namespace Jericdei\PsgcDatabase\Commands; | ||
|
||
use Jericdei\PsgcDatabase\Models\Barangay; | ||
use Jericdei\PsgcDatabase\Models\City; | ||
use Jericdei\PsgcDatabase\Models\Municipality; | ||
use Jericdei\PsgcDatabase\Models\Province; | ||
use Jericdei\PsgcDatabase\Models\Region; | ||
use Jericdei\PsgcDatabase\Models\SubMunicipality; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\Cache; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\File; | ||
use Spatie\SimpleExcel\SimpleExcelReader; | ||
|
||
use function Laravel\Prompts\confirm; | ||
|
||
class ConvertToDatabaseCommand extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'psgc:convert'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Convert PSGC excel data to database.'; | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle() | ||
{ | ||
if (Region::count() !== 0) { | ||
$confirm = confirm('PSGC database is not empty. Do you want to remove all data?'); | ||
|
||
if (!$confirm) { | ||
$this->error('Action cancelled.'); | ||
|
||
return; | ||
} | ||
|
||
DB::table('regions')->truncate(); | ||
DB::table('provinces')->truncate(); | ||
DB::table('cities')->truncate(); | ||
DB::table('municipalities')->truncate(); | ||
DB::table('sub_municipalities')->truncate(); | ||
DB::table('barangays')->truncate(); | ||
|
||
$this->info('All data has been removed.'); | ||
} | ||
|
||
$file = storage_path("app/public/psgc/latest.xlsx"); | ||
|
||
if (!File::exists($file)) { | ||
$this->info('Downloading latest PSGC file...'); | ||
$this->call('psgc:dl-latest'); | ||
} | ||
|
||
$this->info('Reading PSGC Excel file...'); | ||
|
||
$worksheet = Cache::rememberForever( | ||
"psgc-latest", | ||
fn() => SimpleExcelReader::create($file)->fromSheetName('PSGC')->getRows()->toArray() | ||
); | ||
|
||
array_shift($worksheet); | ||
|
||
$this->info('Writing to database...'); | ||
|
||
// Guide | ||
// 0 -> code | ||
// 1 -> name | ||
// 2 -> old_code | ||
// 3 -> type | ||
// 4 -> old_name | ||
foreach ($worksheet as $row) { | ||
$code = $row['10-digit PSGC']; | ||
|
||
if ($code === null) { | ||
continue; | ||
} | ||
|
||
$type = strtolower($row['Geographic Level']); | ||
|
||
$data = [ | ||
'code' => $code, | ||
'old_code' => $row['Correspondence Code'], | ||
'region_code' => mb_substr($code, 0, 2), | ||
'province_code' => mb_substr($code, 2, 3), | ||
'municipality_code' => mb_substr($code, 2, 5), | ||
'city_code' => mb_substr($code, 2, 5), | ||
'sub_municipality_code' => mb_substr($code, 5, 2), | ||
'barangay_code' => mb_substr($code, 2, 8), | ||
'name' => trim($row['Name']), | ||
'old_name' => $row['Old names'] ? trim($row['Old names']) : null, | ||
]; | ||
|
||
DB::transaction(fn() => match ($type) { | ||
'reg' => Region::create($data), | ||
'prov' => Province::create($data), | ||
'mun' => Municipality::create($data), | ||
'city' => City::create($data), | ||
'submun' => SubMunicipality::create($data), | ||
'bgy' => Barangay::create($data), | ||
default => null, | ||
}); | ||
|
||
$this->info("Saved to database: $type - {$data['name']}"); | ||
} | ||
|
||
$this->newLine(2); | ||
$this->info('Done~!'); | ||
} | ||
} |
Oops, something went wrong.