zinapse / laralocate
获取关于各种地点、国家、州、城市等信息。
v1.0.5
2023-04-17 12:54 UTC
Requires
- php: >=8.0
- guzzlehttp/guzzle: ^7.4
- illuminate/support: ^9.7
- laravel/framework: ^9.3
This package is auto-updated.
Last update: 2024-09-17 16:04:37 UTC
README
获取国家、州和城市之间的关系。
概述
此包抓取包含世界各地地点数据的JSON文件,然后将这些数据解析到数据库中。 该文件在此。
安装
-
使用composer包含它:
composer require zinapse/laralocate
-
确保您已运行迁移:
php artisan migrate
- 您可以使用 --pretend 查看将要进行的更改:
php artisan migrate --pretend
。
- 您可以使用 --pretend 查看将要进行的更改:
-
之后,您只需填充数据库:
php artisan laralocate:populate
- 您可以使用
--cities
选项查看详细的市输出。 - 如果您不希望命令自动下载JSON文件,可以指定自己的JSON文件路径:
php artisan laralocate:populate --file=/my/path/to/file.json
(只需确保它遵循相同的结构即可)
- 您可以使用
模型
城市
城市对象有一个名称,以及一个作为父级的州ID。
<?php use Zinapse\LaraLocate\Models\City; $city = City::first(); // Get any City object $state = $city->state; // Get this city's State object $country = $city->country; // Get this city's Country object $zipcodes = $city->getPostalCodes(6); // ['type' => 'findNearbyPostalCodes', 'lat' => $city->lat, 'lng' => $city->long, 'radius' => 6, 'maxRows' => 5]
州
州对象有一个名称,一个州代码,以及一个作为父级的国家ID。
<?php use Zinapse\LaraLocate\Models\State; // Get all states from the country passed $state = State::fromCountry('United States'); $code = $state->code; // The state code $cities = $state->cities; // A collection of City objects from this state $country = $state->country; // This state's Country object
国家
国家对象有一个名称和一个国家代码。
<?php use Zinapse\LaraLocate\Models\Country; $country = Country::where('name', 'United States')->first(); $states = $country->states; // Get a collection of this country's State objects
特征码
特征码对象有一个代码,描述和一个父ID。具有null long_desc
和null parent_id
的行是顶级代码。这些行将有一个包含以|
字符分隔的文本的 short_desc
,这些文本是可能在该代码下的示例区域。
<?php use Zinapse\LaraLocate\Models\FeatureCode; $code = FeatureCode::where('code', 'A')->first(); $examples = explode('|', $code->short_desc); // ['country', 'state', 'region]
GeoNames
GeoNames模型与任何表都不相关,但它包含用于调用GeoName webhook的静态函数。
<?php use Zinapse\LaraLocate\Models\GeoNames; $all_webhooks = GeoNames::GetWebhooks(); // returns an array: ['webhook name' => [required variables to pass], ...] $named_webhook = GeoNames::Webhook(['type' => 'postalCodeSearch', 'postalcode' => 12345]); // runs the postalCodeSearch webhook // Added helper functions so you don't have to remember all this, or pass arrays, etc $search = GeoNames::GeoSearch('Alaska', 2); // ['type' => 'search', 'q' => 'Alaska', 'maxRows' => 2]