abe/chinese-regions-for-laravel

v0.3.0 2024-01-10 19:16 UTC

This package is auto-updated.

Last update: 2024-11-08 11:36:43 UTC


README

Latest Version on Packagist Total Downloads tests Coverage

数据来源

安装

先决条件
  • ext_pdo_sqlite 我们需要读取数据源的 SQLite 文件导入到自己的数据库中,一旦导入完成就可以禁用扩展

然后通过 Composer 安装:

composer require abe/chinese-regions-for-laravel

安装后,运行数据库迁移命令,以创建数据表

php artisan migrate

数据表创建完成后,执行导入命令,导入数据

php artisan chinese-regions:import

使用

我附带了一个简单的实体类, 你也可以自己实现

$chineseRegion = new \Abe\ChineseRegionsForLaravel\ChineseRegion();
/** @var \Illuminate\Database\Eloquent\Collection $provinces */
$provinces = $chineseRegion->provinces; // 所有省份
/** @var \Illuminate\Database\Eloquent\Collection $cities */
$cities = $provinces->first()->cities; // 获取第一个省份的所有城市
/** @var \Illuminate\Database\Eloquent\Collection $areas */
$areas = $cities->first()->areas; // 获取第一个城市的所有区县
/** @var \Illuminate\Database\Eloquent\Collection $streets */
$streets = $areas->first()->streets; // 获取第一个区县的所有街道
/** @var \Illuminate\Database\Eloquent\Collection $villages */
$villages = $streets->first()->towns; // 获取第一个街道的所有乡镇
/** @var \Illuminate\Database\Eloquent\Collection $street */
$street = $villages->parent; // 获取上级
** @var \Illuminate\Database\Eloquent\Collection $villages */
$villages = $street->children; // 获取**下一级**

// 请注意,我们只支持一级下级,平均每个省份有 2 万个村,我不认为有必要返回多级,你可以多次请求接口,或者自行实现

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.