melsaka / world-countries
一个 Laravel 包,允许您轻松地将国家、州和城市添加到您的 Laravel 应用程序中。
v0.1.0
2023-10-31 11:04 UTC
Requires
- php: ^8.1
- laravel/framework: ^10.10
README
World Countries 是一个 Laravel 包,提供了一种简单的方法来检索包含相应州和城市的国家列表。这可以用于各种应用程序,例如创建基于位置的表单、填充下拉列表或任何需要处理地理数据的场景。
安装
通过 Composer 将包添加到您的 Laravel 应用程序中。
composer require melsaka/world-countries
在 config/app.php 中注册包的服务提供者。
'providers' => [ ... Melsaka\WorldCountries\WorldCountriesServiceProvider::class, ... ];
使用
安装包后,您可以在 Laravel 应用程序中开始使用它。
国家
您可以使用 get()
方法访问所有国家数据。
use Melsaka\WorldCountries\Country; $countries = new Country(); // returns a laravel collection of all supported countries. $allCountries = $countries->get(); // returns the data of a specifc country as an array $egypt = $countries->get('eg');
国家数据示例
array:24 [▼ "id" => 65 "name" => "Egypt" "name_ar" => "مصر" "iso3" => "EGY" "iso2" => "EG" "numeric_code" => "818" "phone_code" => "20" "capital" => "Cairo" "currency" => "EGP" "currency_name" => "Egyptian pound" "currency_symbol" => "ج.م" "tld" => ".eg" "native" => "مصر" "region" => "Africa" "region_id" => "1" "subregion" => "Northern Africa" "subregion_id" => "1" "nationality" => "Egyptian" "timezones" => array:1 [▼ 0 => array:5 [▼ "zoneName" => "Africa/Cairo" "gmtOffset" => 7200 "gmtOffsetName" => "UTC+02:00" "abbreviation" => "EET" "tzName" => "Eastern European Time" ] ] "translations" => array:13 [▼ "kr" => "이집트" "pt-BR" => "Egito" "pt" => "Egipto" "nl" => "Egypte" "hr" => "Egipat" "fa" => "مصر" "de" => "Ägypten" "es" => "Egipto" "fr" => "Égypte" "ja" => "エジプト" "it" => "Egitto" "cn" => "埃及" "tr" => "Mısır" ] "latitude" => "27.00000000" "longitude" => "30.00000000" "emoji" => "🇪🇬" "emojiU" => "U+1F1EA U+1F1EC" ]
州
要获取国家州数据
// returns a laravel collection of egyptian states. $egyptianStates = Country::states($egypt); // returns the data of a specifc state in egypt as an array $alexandria = Country::states($egypt, 'ALX');
您还可以通过从 State
对象调用 get()
方法来访问每个国家的州数据。
use Melsaka\WorldCountries\State; $states = new State($egypt); // or $states = new State('EG'); // returns a laravel collection of egyptian states. $egyptianStates = $states->get(); // returns the data of a specifc state in egypt as an array $alexandria = $states->get('ALX');
州数据示例
array:10 [▼ "id" => 3235 "name" => "Alexandria" "name_ar" => "Alexandria" "country_id" => 65 "country_code" => "EG" "country_name" => "Egypt" "state_code" => "ALX" "type" => null "latitude" => "30.87605680" "longitude" => "29.74260400" ]
城市
要获取国家城市数据
// returns a laravel collection of egyptian cities. $egyptianCities = Country::cities($egypt); // returns a laravel collection of egyptian cities that belongs to specifc state in egypt $alexandriaCities = Country::cities($egypt, 'ALX'); // or $alexandriaCities = Country::cities($egypt, $alexandria);
您还可以通过从 City
对象调用 get()
方法来访问每个国家的城市数据。
use Melsaka\WorldCountries\City; $cities = new City($egypt); // or $cities = new City('EG'); // returns a laravel collection of egyptian cities. $egyptianCities = $cities->get(); // returns a laravel collection of egyptian cities that belongs to specifc state in egypt $alexandriaCities = $cities->get('ALX');
城市数据示例
array:9 [▼ "id" => 1140594 "name" => "Abees" "name_ar" => "Abees" "state_id" => 3235 "state_code" => "ALX" "state_name" => "Alexandria" "country_id" => 65 "country_code" => "EG" "country_name" => "Egypt" ]
许可
此包根据 MIT 许可证(MIT)发布。