直接上程式碼吧
This package focused on World Countries, Regions, and Cities database with locale support for Laravel.
Conceptions
There are 5 main objects in this package.
- World: the earth world.
- Continent: 7 continent
- Country: 247 countries
- Region: several countries have region/state/province , e.g. US, UK, and China.
- City: the last level region, some city up to Country, some up to region.
Attributes
Every object has name
, full_name
attributes,
full_name
is not supported for Continent/Region/City.
use Khsing\World\Models\Country;
$china = Country::getByCode('cn');
$china->name; // China, 中國
$china->full_name; // People's Republic of China, 中華人民共和國
$china->code; // CN
$china->code3; // CHN
$china->has_region; // true
Locales
Right now, only English(default and fallback) and zh-cn
are supported. Locale settings is following Laravel project settings in config/app.php
.
Setup
composer require
composer require 'khsing/world:dev-master'
- Add Service Provider into
config/app.php
'providers' => [
// ...
Khsing\World\WorldServiceProvider::class,
]
- Publish and init
php artisan vender:publish
composer dump-autoload
php artisan world:init
Usage
- get all Continent
use Khsing\World\World;
World::getContinents()
- get all Countries
use Khsing\World\World;
World::getCountries()
- get country by code
use Khsing\World\Models\Country;
Country::getByCode('cn');
- get countries belong to a continent
use Khsing\World\Models\Continent;
$asia = Continent::getByCode('AS');
$countries = $asia->countries()->get();
// or use children method
$countries = $asia->children();
- get continent or parent
$china = Country::getByCode('cn');
$asia = $china->parent();
- get region/state/province via Conutry
$china = Country::getByCode('cn');
$provinces = $china->regions()->get()
// or use children method
$provinces = $china->children();
- get cities via Country or Region.
$china = Country::getByCode('cn');
// check has_region to determine next level is region or city.
$china->has_region; // true, otherwise is false
$regsions = $china->children();
About
This package published under MIT license. If you have any question or suggestion, please feel free to submit a issue, or email me Guixing<khsing.cn(AT)gmail.com>.
Have a nice day.
本作品採用《CC 協議》,轉載必須註明作者和本文連結