khsing / laravel-world

provide countries, states, and cities relations and database.
MIT License
436 stars 91 forks source link
city continent country database division laravel region state translation world

Laravel World Database

This package focused on World Countries, Regions, and Cities database with locale support for Laravel.

Conceptions

There are 5 main objects in this package.

Attributes

Common attributes:

Country spec attributes:

Example:

use Khsing\World\World;
$china = World::getByCode('cn');
$china->setLocale('zh-cn');
$china->name; // China
$china->local_name; // 中国
$china->full_name; // People's Republic of China
$china->local_full_name; // 中华人民共和国
$china->emoji; // 🇨🇳
$china->callingcode; // 86
$china->code; // CN
$china->code_alpha3; // CHN
$china->has_division; // true
$china->currency_code; // CNY
$china->currency_name; // Yuan Renminbi
$china->local_currency_name; // 人民币

Localization

Right now, only English(default and fallback) and Chinese-Simp zh-cn are supported. Locale settings is following Laravel project settings in config/app.php.

Setup

composer require khsing/world
'providers' => [
    // ...
    Khsing\World\WorldServiceProvider::class,
]
php artisan vendor:publish --force --provider="Khsing\World\WorldServiceProvider"
composer dump-autoload
php artisan world:init

Usage

use Khsing\World\World;

World::Continents()
use Khsing\World\World;

World::Countries()
use Khsing\World\World;

World::getByCode('cn'); // iso-3166 alpha 2 code
World::getByCode('chn'); // iso-3166 alpha 3 code
World::getByCode('cn-11'); // Beijing
use Khsing\World\Models\Continent;

$asia = Continent::getByCode('AS');
$countries = $asia->countries()->get();
// or use children method
$countries = $asia->children();
$china = Country::getByCode('cn');
$asia = $china->parent();
$china = Country::getByCode('cn');
$provinces = $china->divisions()->get()
// or use children method
$provinces = $china->children();
$china = Country::getByCode('cn');
// check has_division to determine next level is division or city.
$china->has_division; // true, otherwise is false
$regsions = $china->children();

Contributions

If you want contribute to this library, issue and pr are welcome. please following those steps.

  1. start a new laravel project and install this library.
  2. install orangehill/iseed.
  3. modify datas via sql.
  4. generate seeds via artisan iseed world_cities,world_cities_locale,world_continents,world_continents_locale,world_countries,world_countries_locale,world_divisions,world_divisions_locale
  5. replace delete() with truncate(), cd database/seeders/ && sed -i 's/->delete()/->truncate()/g' World*.php
  6. copy seeds files into library.
  7. commit your work. ;)

TODO

Data Sources

Thanks

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.