在 Laravel 上撸了一个支持多语言的国家地区数据库

直接上代码吧

khsing/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.

  • 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 协议》,转载必须注明作者和本文链接
本帖由 Summer 于 8年前 加精
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 4
hellloveyy

阿贵哥还是这么叼~~~

8年前 评论
jacktop

不支持laravel9 ??

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!