在 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 于 6年前 加精
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 4
hellloveyy

阿贵哥还是这么叼~~~

6年前 评论
jacktop

不支持laravel9 ??

2年前 评论

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