dileedotdev / laravel-vietnamese-administrative-units
A Laravel package for interacting with Vietnamese administrative units
Requires
- php: ^8.2
- illuminate/contracts: ^10.0
- spatie/laravel-package-tools: ^1.14.0
- spatie/simple-excel: ^3.2
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.9
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^8.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- spatie/test-time: ^1.3
This package is auto-updated.
Last update: 2024-12-28 08:32:45 UTC
README
This package provides a simple way to interact with Vietnamese administrative units in Laravel by normal models and relationships.
Lasted updated Vietnamese administrative units CSV file was on 13/5/2023
Installation
You can install the package via composer:
composer require dileedotdev/laravel-vietnamese-administrative-units
You should publish and run the migrations with:
php artisan vendor:publish --tag="vietnamese-administrative-units-migrations"
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --tag="vietnamese-administrative-units-config"
This is the contents of the published config file:
return [ 'province' => [ 'model' => \VietnameseAdministrativeUnits\Models\Province::class, 'table' => 'provinces', ], 'district' => [ 'model' => \VietnameseAdministrativeUnits\Models\District::class, 'table' => 'districts', ], 'ward' => [ 'model' => \VietnameseAdministrativeUnits\Models\Ward::class, 'table' => 'wards', ], ];
Usage
As you can see, the package provides 3 models: Province
, District
, and Ward
. Each model uses soft delete. (because in Vietnam, administrative units probably change in the future)
Import data
The first thing you need to do is import administrative units to the database. You can use the command:
php artisan vietnamese-administrative-units:import
By default, this command will use a CSV file placed in ./assets/vietnamese-administrative-units.csv
. You can change the file using by passing a path to the file as an argument:
php artisan vietnamese-administrative-units:import /home/dilee/Downloads/vietnamese-administrative-units.csv
If you wonder where the CSV file comes from, you can find it here check on "Quận Huyện, Phường Xã" and click "Xuất Excel" to download the Excel file, next you should manually convert the Excel file to CSV file without changing any data.
Using models
You can use the models as usual:
use VietnameseAdministrativeUnits\Models\Ward; class YourModel { public function ward() { return $this->belongsTo( config('vietnamese-administrative-units.ward.model', Ward::class) )->withTrashed(); } }
As I said before, each model uses soft delete, so you should use withTrashed()
to get a relationship regardless the model is deleted or not.
And don't forget to define a foreign key in your migration:
return new class extends Migration { public function up() { Schema::table('your_table', function (Blueprint $table) { $table->foreignId('ward_id')->constrained(); }); } }
Below is some useful code:
use VietnameseAdministrativeUnits\Models\Province; use VietnameseAdministrativeUnits\Models\District; use VietnameseAdministrativeUnits\Models\Ward; // Get all provinces Province::all(); // Get all districts District::all(); // Get all wards Ward::all();
Can I use the import command many times?
Yes, you can. The command is smart enough to check if the administrative unit is already in the database, it will skip that unit and soft delete any unit that is not in the CSV file.
What I should do if the CSV file I provided is outdated?
I will periodically update the CSV file, so rarely you need to update the CSV file yourself. But if the CSV file is outdated, you can follow the below solutions:
Firstly, the command optionally receives the custom path to the CSV file, so you can download the new CSV file and pass it to the command.
Secondly, you can go to the Github repository of this package, create a pull request to update the CSV file, and I will merge it as soon as possible.
Finally, if the above solutions are too complicated for you, easily you can create an issue on Github to ping me to update the CSV file.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.