kevinpurwito/laravel-country

Laravel国家列表迁移、种子文件和模型

v3.0.0 2022-05-24 07:18 UTC

This package is auto-updated.

Last update: 2024-09-12 19:59:15 UTC


README

Tests Code Style Psalm Latest Version on Packagist Total Downloads

Laravel Country是一个包含国家列表迁移、种子文件和模型的包。

国家列表从https://countrycode.org/获取

安装

您可以通过composer安装此包

composer require kevinpurwito/laravel-country

配置

vendor:publish命令会在您的Laravel项目配置文件夹config/kp_country.php中发布一个名为kp_country.php的文件。请编辑此文件,指定您想要的表名,默认为countries

发布配置文件内容

[
    'table_names' => [
        'country' => env('KP_COUNTRY_TABLE_NAME', 'countries'),

        'province' => env('KP_COUNTRY_TABLE_NAME_PROVINCE', 'provinces'),

        'city' => env('KP_COUNTRY_TABLE_NAME_CITY', 'cities'),

        'district' => env('KP_COUNTRY_TABLE_NAME_DISTRICT', 'districts'),

        'ward' => env('KP_COUNTRY_TABLE_NAME_WARD', 'wards'),
    ],

    'popular_column' => env('KP_COUNTRY_POPULAR_COLUMN', true),

    'ordinal_column' => env('KP_COUNTRY_ORDINAL_COLUMN', true),
];

或者,您可以忽略上述发布命令,并将以下变量添加到您的.env文件中。

KP_COUNTRY_TABLE_NAME=countries
KP_COUNTRY_TABLE_NAME_PROVINCE=provinces
KP_COUNTRY_TABLE_NAME_CITY=cities
KP_COUNTRY_TABLE_NAME_DISTRICT=districts
KP_COUNTRY_TABLE_NAME_WARD=wards
KP_COUNTRY_POPULAR_COLUMN=true
KP_COUNTRY_ORDINAL_COLUMN=true

自动发现

如果您使用的是Laravel 5.5+,您不需要手动添加服务提供者或外观。这将会自动发现。对于所有低于5.5版本的Laravel,您必须在您的Laravel项目config/app.php文件中适当数组中手动添加ServiceProvider & Facade。

服务提供者

[
    Kevinpurwito\LaravelCountry\CountryServiceProvider::class,
];

运行迁移

您只需要发布迁移,不需要发布其他内容,例如种子文件和配置;除非您想要自定义它们。

仅国家迁移

php artisan vendor:publish --provider=Kevinpurwito\\LaravelCountry\\CountryServiceProvider --tag=lc-countries

所有迁移(国家、省份、城市、地区、街区)

php artisan vendor:publish --provider=Kevinpurwito\\LaravelCountry\\CountryServiceProvider --tag=lc-migrations

运行种子文件

国家种子文件

php artisan db:seed --class=Kevinpurwito\\LaravelCountry\\Database\\Seeders\\CountriesSeeder

国家code来自ISO 3166

印尼省份、城市和地区种子文件

php artisan db:seed --class=Kevinpurwito\\LaravelCountry\\Database\\Seeders\\IdProvincesSeeder
php artisan db:seed --class=Kevinpurwito\\LaravelCountry\\Database\\Seeders\\IdCitiesSeeder
php artisan db:seed --class=Kevinpurwito\\LaravelCountry\\Database\\Seeders\\IdDistrictsSeeder

印尼省份iso2来自ISO 3166-2:ID

印尼省份、城市和地区code来自Kemendagri

用法

国家类

use Kevinpurwito\LaravelCountry\Models\Country;

// Get a country by name, iso2 or iso3
$country = Country::findByName('Indonesia');
$country = Country::findByIso2('ID');
$country = Country::findByIso3('IDN');

// mark a country as popular
$country->setPopular(true);

// set `ordinal` of the country
$country->setOrdinal(1);

// get list of countries by default ordering (`popular` first, after that by `ordinal` and finally by `name`)
$countries = Country::default()->get();

// get list of provinces in a country
$provinces = $country->provinces()->default()->get();

// get list of cities in a province
$cities = $provinces[0]->cities()->default()->get();

将关系添加到您的模型中

use Kevinpurwito\LaravelCountry\Relationships\BelongsToCountry;
use Illuminate\Database\Eloquent\Model;

class User extends Model 
{
    use BelongsToCountry;
    
    // now you can use `country` relationship to your user model, given that your table has 'country_id' column
    // e.g. $user->country->iso2; returns 'ID' if your user belongs to 'Indonesia' country
}

测试

composer test

变更日志

请参阅CHANGELOG以获取有关最近更改的更多信息。

贡献

请参阅CONTRIBUTING以获取详细信息。

安全

如果您发现任何安全问题,请通过kevin.purwito@gmail.com发送电子邮件,而不是使用问题跟踪器。

鸣谢

许可

MIT许可证(MIT)。请参阅许可文件以获取更多信息。

Laravel包模板

此包是使用PHP包模板Beyond Code生成的,并对PHP包骨架进行了修改,该骨架由spatie提供灵感。