Reference for everything the package exposes at version 2.2.2. Namespace root: Webpatser\Countries.
Try it
Result
{
"name": "United States",
"full_name": "United States of America",
"capital": "Washington, D.C.",
"iso_3166_2": "US",
"iso_3166_3": "USA",
"currency_code": "USD",
"currency_name": "United States dollar",
"currency_symbol": "$",
"calling_code": "1",
"region": "americas",
"languages": [
"en"
],
"flag": "\ud83c\uddfa\ud83c\uddf8"
}
Executed PHP Code:
$countries = new Countries();
$result = $countries->getOne('US');
About getOne($code)
Retrieves a single country by its ISO code (2 or 3 letter). Returns null if country not found.
Countries Class
Webpatser\Countries\Countries is registered as a singleton under countries and Countries::class. The Countries facade alias points at Webpatser\Countries\CountriesFacade.
| Method | Description |
|---|---|
getOne(string $iso): ?array |
One country by code, case-insensitive; null if unknown |
getList(?string $sort = null): array |
All countries keyed by code; optional sort field |
getByCurrency(string $currencyCode): array |
Countries using the currency, case-insensitive |
getByRegion(string $region): array |
Countries in the region, case-insensitive exact match |
search(string $query): array |
Substring match on name, full_name and capital, case-insensitive |
getListForSelect(string $display = 'name'): array |
[code => value of $display], sorted by that field |
collect(): Collection |
The list as a Laravel Collection |
clearCache(): void |
Forgets the countries_data cache key and resets the instance copy. In 2.2.2 the list is cached under countries_data_json or countries_data_database, so this does not remove it; use Cache::forget('countries_data_json') or set cache_ttl to 0 |
Valid getList() sort fields: name, capital, iso_3166_2, iso_3166_3, currency_code, currency_name, currency_symbol, calling_code, region, languages, flag. Any other value returns the unsorted list.
use Webpatser\Countries\Countries; $countries = new Countries(); $countries->getOne('US');$countries->getList('name');$countries->getByCurrency('EUR');$countries->getByRegion('Europe');$countries->search('united');$countries->getListForSelect('name');$countries->collect()->where('region', 'europe');
Country Data Structure
[ 'name' => 'United States', // common name 'full_name' => 'United States of America', // official name (2.2.0+) 'capital' => 'Washington, D.C.', 'iso_3166_2' => 'US', // 2-letter code 'iso_3166_3' => 'USA', // 3-letter code 'currency_code' => 'USD', 'currency_name' => 'United States dollar', 'currency_symbol' => '$', 'calling_code' => '1', 'region' => 'americas', // africa, americas, asia, europe, oceania 'languages' => ['en'], // language codes 'flag' => '🇺🇸',]
With data_source set to database, the arrays built by Countries have the same keys except full_name.
Configuration
| Key | Default |
|---|---|
table_name |
countries |
cache_ttl |
3600 |
data_source |
json |
default_sort |
name |
localized |
false |
search.fields |
['name', 'full_name', 'capital'] |
search.case_sensitive |
false |
Note:
default_sort,search.fieldsandsearch.case_sensitiveexist in the published file but are not read by the package in 2.2.2.
Publish with php artisan vendor:publish --tag=countries-config.
Artisan Commands
| Command | Options | Purpose |
|---|---|---|
countries:install |
--table-name=, --force |
Interactive setup: migration, seeder, import, config |
countries:migration |
--force |
Creates the migration and seeder files |
Helper Functions
| Function | Description |
|---|---|
country_name($code) |
Common name |
country_flag($code) |
Flag emoji |
country_capital($code) |
Capital city |
country_currency($code) |
['code' => ..., 'name' => ..., 'symbol' => ...] |
country_region($code) |
Region |
country_languages($code) |
Language codes |
country_calling_code($code) |
Calling code with + |
country_formatted($code, $includeFlag = true) |
Flag and name |
country_exists($code) |
bool |
country_code_from_name($name) |
Code from common or official name |
countries_by_region($region) |
Countries in a region |
countries_by_currency($currency) |
Countries using a currency |
countries_search($query) |
Search by common name, official name (full_name) or capital |
countries_select_options($region = null, $includeFlags = true) |
Select options |
country_code_to_flag($code) |
Code to flag emoji |
flag_to_country_code($flag) |
Flag emoji to code |
Details: Helper Functions.
Collection Macros
| Macro | Description |
|---|---|
byCountry($code) |
Filter on country_code |
byRegion($region) |
Filter on region (contains) |
byCurrency($currency) |
Filter on currency_code |
searchCountries($query) |
Search name, capital and ISO codes |
withFlags() |
Add display_name |
countryNames() |
Pluck name |
countryCodes() |
Pluck iso_3166_2 |
countryFlags() |
Pluck flag |
uniqueRegions() |
Sorted unique regions |
uniqueCurrencies() |
Sorted unique currency codes |
Details: Collection Macros.
String Macros
| Method | Description |
|---|---|
Str::toCountryFlag($code) |
Code to flag emoji |
Str::fromCountryFlag($flag) |
Flag emoji to code |
Str::countryName($code) |
Common name, empty string if unknown |
Validation Rules
| Rule | Constructor |
|---|---|
Rules\ValidCountryCode |
new ValidCountryCode(string $format = 'iso_3166_2'); formats iso_3166_2, iso_3166_3, name |
Rules\ValidCurrencyCode |
new ValidCurrencyCode() |
Rules\ValidRegion |
new ValidRegion() |
Details: Validation Rules.
Country Model
Models\Country, primary key iso_3166_2.
| Member | Description |
|---|---|
scopeByRegion($region) |
region LIKE %region% |
scopeByCurrency($code) |
Currency code equals the upper-cased code |
scopeByLanguage($language) |
languages LIKE %language% |
scopeSearch($search) |
LIKE on name, full name, capital and both ISO codes |
scopeEuropean() |
region = 'Europe' (exact). The seeded data is lowercase, so this returns no rows on case-sensitive databases such as PostgreSQL or SQLite; use byRegion('europe') |
scopeWithCurrency() |
Currency code is not null |
name_with_flag |
Name, space, flag |
formatted_calling_code |
Calling code with + |
languages_list |
Languages joined with , , or N/A |
hasLanguage($language) |
bool |
hasCurrency() |
bool |
isInRegion($region) |
bool, partial case-insensitive match |
toSelectOption() |
['value' => code, 'label' => name_with_flag] |
Country::getSelectOptions(?string $region = null) |
Select options, ordered by name |
Traits and Casts
| Class | Members |
|---|---|
Traits\HasCountry |
country() relation, country_name, country_flag, formatted_country, fromCountry(), fromRegion(), fromCurrency(), isFromCountry(), isFromRegion(), isFromEurope(), isFromAsia(), isFromAfrica(), isFromNorthAmerica(), isFromSouthAmerica(), isFromOceania() |
Traits\CountrySelectable |
Static getCountrySelectOptions(?string $region = null, bool $includeFlags = true), getRegionSelectOptions(), getCurrencySelectOptions(), getLanguageSelectOptions() |
Casts\CountryCode |
Upper-cases on read; upper-cases and trims on write |
Casts\CountryFlag |
Returns the flag emoji, builds it from iso_3166_2 for single-character values |
Casts\CountryLanguages |
JSON array to PHP array |
Details: Eloquent Models.
HTTP Classes
| Class | Description |
|---|---|
Http\Middleware\ValidateCountryCode |
Reads the country route parameter (configurable); JSON 400 if missing, 404 if unknown, otherwise adds validated_country to the request |
Http\Middleware\LocalizeByCountry |
Reads the country route parameter (configurable) and sets the app locale from a built-in country-to-locale map (for example DE to de, NL to nl). The route value must be upper case; lower-case codes leave the locale unchanged |
Http\Requests\CountryRequest |
Validation for a full country record |
Http\Requests\CountrySearchRequest |
Validation and getters for search, sort and pagination parameters |
The service provider does not register middleware aliases. Reference the classes directly in your routes.
Next Steps
- Helper Functions: Reference for every helper function.
- Installation: Back to the installation guide.