human018 / laravel-earth
一个多语言Laravel包,用于填充地球上的大洲、国家、地区和城市
Requires
- php: >=8.0
- laravel/framework: ^8.0|^9.0|^10.0|^11.0
- npm-asset/country-region-data: ^1.7.0
- staudenmeir/eloquent-has-many-deep: ^1.7
README
关于本项目
一个多语言Laravel包,用于填充地球上的大洲、国家、地区、城市、语言和货币。
使用技术
- PHP
- Laravel
入门
此项目使用Laravel 8构建,目前不支持旧版本。
先决条件
- PHP 7.4
- Laravel 8, 9, 10或11
- Guzzle
- ZipArchive
- Countrylayer API
由于此库使用NPM包作为其数据源之一,您需要允许composer下载NPM包。为此,请将以下属性添加到您的composer.json文件中的repositories
数组/对象中。
"repositories": [ { "type": "composer", "url": "https://asset-packagist.org" } ]
API访问
您需要通过countrylayer获取API密钥才能使用其服务。免费密钥可用,并且仅用于种子。一旦您拥有API密钥,请将其添加到您的.env文件中。
COUNTRY_LAYER_API=XXX
安装
-
使用composer将包安装到您的项目中。
composer require human018/laravel-earth
-
运行数据库迁移。
php artisan migrate
-
此包使用以下列出的一些来源对数据库进行种子,因此需要运行初始化脚本才能访问这些第三方来源并填充您的数据库。
php artisan earth:init
-
(可选) 如果您想填充某个国家的所有城市,请传递国家代码。请注意,这可能取决于您要填充的国家而花费较长时间。
php artisan earth:init --country=au
-
(可选) 您还可以填充所有主要城市(人口超过15,000)。
php artisan earth:init --cities=major
用法
所有模型都位于相同的命名空间中,并可以导入到您的本地项目中。
use Human018\LaravelEarth\Models\Continent; use Human018\LaravelEarth\Models\Country; use Human018\LaravelEarth\Models\Region; use Human018\LaravelEarth\Models\City; use Human018\LaravelEarth\Models\Language; use Human018\LaravelEarth\Models\Currency; use Human018\LaravelEarth\Models\Timezone; use Human018\LaravelEarth\Models\TimezoneUTC;
包括一些有用的方法来查找正确的资源。这意味着资源名称或代码可以用来快速定位资源。此外,模型之间的关系以标准方式连接,可以像通常一样链式操作。
// eg 1. Retrieve country by code $country = Country::code('au'); echo $country->regions->count(); // Returns '8' // eg 2. You can pass in the Code in lower or upper case $country = Country::code('AU'); // Eg 3. You can also search by name $region = Region::name('New South Wales'); echo $region->country->name; // Returns 'Australia' echo $region->country->capital->name; // Returns 'Canberra'
检索时区稍微复杂一些。当前时区与国家相关联,而不是地区或城市。由于一个国家可以有多个UTC时区,而这些时区可以属于同一个时区,检索关系只需要我们使用唯一的查询修饰符。
$country = Country::code('ca'); // Canada $country->timezones->pluck('label'); // Returns duplicates because it's unique UTC zones // belong to many of the same Timezones $country->timezones->unique()->pluck('label'); // Returns only unique timezones
贡献
贡献使开源社区成为一个学习、灵感和创造的美好地方。您所做的任何贡献都将被高度赞赏。
- 分支项目
- 创建您的功能分支 (
git checkout -b feature/AmazingFeature
) - 提交您的更改 (
git commit -m 'Add some AmazingFeature'
) - 将分支推送到远程仓库 (
git push origin feature/AmazingFeature
) - 打开Pull Request
许可证
在MIT许可证下分发。有关更多信息,请参阅LICENSE
。
联系方式
Simon Woodard - @human018
项目链接: https://github.com/Human018/laravel-earth