anandpilania/laravel-country.io

laravel 的 Country.io 数据库

v2.1.0 2021-03-29 11:14 UTC

This package is auto-updated.

Last update: 2024-09-14 11:26:29 UTC


README

Hits

更新(v2.0.4)

  • 您自己的 migration 名称,

  • 从配置中设置模型,

  • 性能改进。

  • v2.0.3

    • 仅保留所需列,
    • 通过 config 文件直接控制。

Laravel-Country.io - 基于 artisan 的包,可以获取包含详细信息的国家列表,即

  • 代码
  • 洲际
  • 首都
  • 国内生产总值(GDP)
  • 人口
  • ISO2/3
  • 语言
  • 人口

等等,直接从 Country.io 获取并直接存储到 file|db

安装

$ composer require anandpilania/laravel-country.io

第一步

$ php artisan vendor:publish --tag=countryio-config

并根据需要配置 config

可选步骤

$ php artisan vendor:publish --tag=countryio-migration

在使用之前

根据需要配置/调整 countryio 配置文件

  - table_name          - table name for storing in DB,
  - model               - Define your own `Model` class [\App\Models\Country::class],
  - cols_type           - `plain|json` [`plain` aka `continent`, `population_total` AND `json` aka `geography.continent`, `population.total`],
  - cols                - `cols_type` free columns,
  - cols_plain|json     - `cols_type` based columns,
  - file                - If not DB, than location of file (`--to=file|db` option of `countryio` artisan command)

如何使用

首先查看支持的 options

$ php artisan help countryio
--to      : file OR db
--offline : save fetched files to `storage/country` dir
--clean   : clean everything after setup
--fresh   : Fresh install (for `db` use only, truncates)
--silent  : silent work

注意:默认位置是 storage/app/countryio.json

因此,如果您只想获取 file 列表(默认):首先发布 config 文件

$ php artisan vendor:publish --tag="countryio-config"

然后,更改位置: config/countryio.php

'file' => storage_path('app/...');

并执行

php artisan countryio
针对 db

anandpilania/laravel-country.io 包含国家 migrationmodel

php artisan countryio --to=db

将创建 migration table(基于 countryio 配置)和 CountryIO 模型(默认值,如果配置文件中没有设置)并直接更新数据库。

为什么不使用 migration publish:因为如果您的应用程序已经有了 country 的迁移/模型,那么...

待办事项

  • 应用使用基于 filetable
  • 旗帜

artisan 玩耍

添加到 routes/console.php

Artisan::command('countries', function () {
    $count = $this->ask('How many entries?');
    $model = config('countryio.model', \App\Models\CountryIO::class);

    if(!class_exists($model)) {
        $this->error($model . ' not exists!');
    }

    foreach ((new $model)->take($count ?? 10)->get() as $c) {
        $output = '';
        foreach (array_merge(config('countryio.cols', []), config('countryio.cols_' . config('countryio.cols_type', 'plain'), [])) as $col => $enabled) {
            if ($enabled) {
                $output .= $c->{$col} . ', ';
            }
        }

        $this->comment($output);
    }
})->purpose('Check CountryIO');

并运行 php artisan countries :)

许可证

MIT

免费软件,太棒了!