coldcoder/laravel-worldcities

一个用于检索世界各地的城市的Laravel扩展包

0.1.1 2018-10-26 04:42 UTC

This package is auto-updated.

Last update: 2024-09-26 17:56:52 UTC


README

根据您的区域设置提供全新的世界国家/州/城市数据库。

在我开发国家、州和城市选择的级联下拉菜单时,我发现了一个名为 khsing/laravel-world 的包,但这个包似乎已经很久没有维护了,所以我 fork 了它并做了一些修改以修复一些问题。由于其数据结构,使用 eager loading 获取区域数据有些困难。因此,我开发了此包,它实现了 spatie/laravel-translatable,这样您就可以轻松地通过区域获取数据。

要求

此包需要Laravel 5.5或更高版本,PHP 7.0或更高版本,以及支持json字段(如MySQL 5.7或更高版本)的数据库。

如果您使用的是Mariadb,请确保版本 > 10.2.8,支持json字段,并使用 ybr-nx/laravel-mariadb 作为您的数据库驱动程序。

安装

您可以通过 composer 安装此包。

composer require coldcoder/laravel-worldcities

该包将自动为 laravel >= 5.5 注册自身。

如果您使用的是 laravel < 5.5,您可以手动在 config/app.php 中注册服务提供者。

您可以使用以下命令发布迁移

php artisan vendor:publish --provider="Coldcoder\WorldCity\WorldCityServiceProvider"

之后,迁移和种子将为您发布,您可以运行迁移和种子来创建 world 结构的表和种子。

php artisan migrate
composer dump-autoload
php artisan db:seed --class=WorldsTablesSeeder

您可以选择使用以下命令发布配置文件

php artisan vendor:publish --provider="Coldcoder\WorldCity\WorldCityServiceProvider" --tag="config"

您可以在发布的配置文件中配置表名。

return [

    /*
     * define the table names for continent, country, state and city
     */
    'table' => [
        'continent' => 'worlds_continents',

        'country' => 'worlds_countries',

        'state' => 'worlds_states',

        'city' => 'worlds_cities',
    ],
];

以下是一些代码示例

use Coldcoder\WorldCity\Models\Country;

// get a country by code
$usa = Country::findFromCode('us');
$usa->states;  // return states
$usa->cities;  // return cities
$usa->has_state; // return true;

// as it just implements locales of en and zh, you can translate other locales by yourself
// or request a PR
// translating a continent/country/state/city
$usa->setTranslation('name', 'fr', 'country name in French');
$usa->save();

// you can use HasCity trait within your own model to setup relationship
use Coldcoder\WorldCity\Traits\HasCity;

class YourModel extends Model
{
    use HasCity;
}

// after that you can get your model's related city
$model->city;

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

贡献

有关详细信息,请参阅 CONTRIBUTING

许可

MIT许可(MIT)。有关更多信息,请参阅 许可文件