thedevsaddam/world-countries

提供包含国家代码、城市、州和国旗的世界国家列表

1.1 2016-08-31 04:54 UTC

This package is not auto-updated.

Last update: 2024-09-23 13:31:43 UTC


README

此包包含世界国家列表,包括州、城市、国家代码和国旗。

安装

通过Composer

$ composer require thedevsaddam/world-countries

手动安装(将以下行添加到composer.json文件中)

"thedevsaddam/world-countries": "1.1"

打开您的终端并运行以下命令

composer update

将以下行添加到config/app.php文件中

Thedevsaddam\WorldCountries\WorldCountriesServiceProvider::class,

以发布配置和资源

php artisan vendor:publish
如果您只需要国旗,则避免以下命令。当您需要使用包含州、城市、国家代码等的国家时,将需要该命令
php artisan world-countries:populate

上述命令将填充国家和其他表到数据库中

要删除填充的表

php artisan world-countries:drop

功能

  1. 包含官方名称、简称、国旗、纬度和经度的世界国家列表
  2. 包含州和城市的全球国家列表
  3. 世界时区(即将推出)

用法

获取所有带国旗的国家。这将返回一个集合对象,以便您可以使用集合类的可用方法

WorldCountries::getCountriesWithFlag();
示例
$countries = WorldCountries::getCountriesWithFlag();
foreach($countries as $key => $country){
    echo "<img src='".$country['flagLargeUrl']."' />";
    echo $country['name'];
    echo "<br/><hr/>";
}
// you can use all the method of Collection class

$country = WorldCountries::getCountriesWithFlag()->where('name', 'Bangladesh');

获取所有带有州和城市的国家

$countries = WorldCountries::getCountriesWithStateCity();

//Note: This query will take some time to fetch all the countries with its associate states and cities

获取Eloquent模型以执行自定义查询

$countryModel = WorldCountries::getCountryModel();

//Fetch all the countries in array
$countries = $countryModel->get()->toArray();

//Fetch all the countries with states
$countriesWithStates = $countryModel->with('states')->get();

//Note: you can use Laravel eloquent available methods (See the below example)

$countryModel = WorldCountries::getCountryModel();
$country = $countryModel->where('name', 'Bangladesh')->first();

注意:您还可以获取城市和州模型。要获取它们,请使用以下方法

$stateModel = WorldCountries::getStateModel();
$cityModel = WorldCountries::getCityModel();
//now you can perform any query available to Eloquent model
此包的最佳用途是用国家填充下拉菜单。
//first get all the countries and populate the country dropdown
$countryModel = WorldCountries::getCountryModel();
$countries = $countryModel->lists('name', 'id');

//you can make an endpoint to fetch all the states against some individual country
$stateModel = WorldCountries::getStateModel();
$states = $stateModel->where('country_id', $someRequest->countryId)->get()->toArray();
return response()-json($states);

// now make another endpoint against the state and fetch all of it's cities.
$cityModel = WorldCountries::getCityModel();
$cities = $cityModel->where('state_id', $someRequest->stateId)->get()->toArray();
return response()->json($cities);
配置

如果您更改国旗目录的名称或路径,则更新config/flag.php文件中的_file_path以根据路径名称进行更新

鸣谢
国旗和国家列表JSON来自:cristiroma
国家、州和城市mysql转储来自:cristiroma

谢谢 :)