The Countries class is the main entry point. The same data is also reachable through the Countries facade, helper functions, collection macros and the Country model.
Basic Usage
use Webpatser\Countries\Countries; $countries = new Countries(); // Get a single country (the code is case-insensitive)$usa = $countries->getOne('US');echo $usa['name']; // "United States"echo $usa['full_name']; // "United States of America"echo $usa['flag']; // "πΊπΈ"echo $usa['capital']; // "Washington, D.C." // Unknown codes return null$countries->getOne('XX'); // null // Get all countries, keyed by ISO 3166-1 alpha-2 code$allCountries = $countries->getList();echo count($allCountries); // 249 // Sorted by a field$byCapital = $countries->getList('capital');
The facade resolves to the same singleton:
use Webpatser\Countries\CountriesFacade as Countries; Countries::getOne('NL')['name']; // "Netherlands"
Try it
United States
US β’ USA
Currency Information
Generated Code:
$countries = new Countries();
$country = $countries->getOne('US');
echo $country['name']; // "United States"
echo $country['flag']; // "πΊπΈ"
echo $country['capital']; // "Washington, D.C."
Filtering and Searching
Filter by Currency
$euroCountries = $countries->getByCurrency('EUR'); foreach ($euroCountries as $code => $country) { echo $country['flag'] . ' ' . $country['name'] . PHP_EOL;}// π¦π© Andorra// π¦πΉ Austria// π¦π½ Γ
land Islands// π§πͺ Belgium// ...
Filter by Region
Regions in the data are africa, americas, asia, europe and oceania. Matching is case-insensitive.
$europeanCountries = $countries->getByRegion('Europe');$asianCountries = $countries->getByRegion('Asia');
Search Countries
search() matches the common name, the official name (full_name) and the capital, case-insensitive:
$searchResults = $countries->search('united'); // Includes United States, United Kingdom, United Arab Emirates,// and countries whose official name contains "united"$countries->search('Hellenic'); // Greece, through its full_name
Try it
Andorra
AD
Albania
AL
Austria
AT
Γ land Islands
AX
Bosnia and Herzegovina
BA
Belgium
BE
Bulgaria
BG
Belarus
BY
Switzerland
CH
Cyprus
CY
Czechia
CZ
Germany
DE
Generated PHP Code:
$countries = new Countries();
$result = $countries->getByRegion('Europe');
// Found 52 countries
Helper Functions
country_name('US'); // "United States"country_flag('US'); // "πΊπΈ"country_capital('US'); // "Washington, D.C."country_formatted('US'); // "πΊπΈ United States" $currency = country_currency('US');// ['code' => 'USD', 'name' => 'United States dollar', 'symbol' => '$'] country_exists('US'); // truecountry_exists('ZZ'); // false country_code_to_flag('DE'); // "π©πͺ"flag_to_country_code('π―π΅'); // "JP"
See Helper Functions for the full list.
Collection Macros
use Webpatser\Countries\Countries; $countries = collect((new Countries())->getList()); $europeanCountries = $countries->byRegion('Europe');$euroCountries = $countries->byCurrency('EUR');$searchResults = $countries->searchCountries('united');$countryNames = $countries->countryNames();$flags = $countries->countryFlags(); $result = $countries ->byRegion('Europe') ->byCurrency('EUR') ->countryNames() ->sort();
String Macros
use Illuminate\Support\Str; Str::toCountryFlag('US'); // "πΊπΈ"Str::fromCountryFlag('π©πͺ'); // "DE"Str::countryName('FR'); // "France"
Form Validation
use Webpatser\Countries\Rules\ValidCountryCode;use Webpatser\Countries\Rules\ValidCurrencyCode;use Webpatser\Countries\Rules\ValidRegion; $request->validate([ 'country' => ['required', new ValidCountryCode('iso_3166_2')], 'currency' => ['nullable', new ValidCurrencyCode()], 'region' => ['nullable', new ValidRegion()],]);
Common Use Cases
Country Dropdown for Forms
// Keyed by code, sorted by name, flag in front of the name$countryOptions = countries_select_options(); // Without flags$countryOptions = countries_select_options(null, false); // Only European countries$europeanOptions = countries_select_options('Europe');
User Profile with Country
HasCountry expects a country_code column on your model and needs the countries table, because it uses the Country Eloquent model.
use Illuminate\Database\Eloquent\Model;use Webpatser\Countries\Traits\HasCountry; class User extends Model{ use HasCountry;} $user->country->name; // Country name$user->country_name; // Accessor$user->formatted_country; // "United States πΊπΈ"$user->isFromEurope(); // bool
Analytics by Region
$usersByRegion = User::all() ->groupBy(fn ($user) => country_region($user->country_code)) ->map->count();
Performance Notes
cache_ttl in config/countries.php controls caching of the loaded country list (default 3600 seconds, 0 disables it). With data_source set to database, with cache_ttl at 0 each Countries instance queries the whole table once; with cache_ttl above 0 the list comes from the cache.
// config/countries.php'cache_ttl' => 3600,
For Eloquent, eager load the relation and select only the columns you need:
use Webpatser\Countries\Models\Country; $users = User::with('country')->get();$countries = Country::select('iso_3166_2', 'name', 'flag')->get();
Next Steps
- Eloquent Models: The Country model and relationships.
- Validation Rules: Validate country data in your forms.
- API Reference: Every class, method and function.