~/docs
Resonate
Resonate
2.x 2.2.2, current
Laravel Countries
Toggle sidebar
Laravel Countries
webpatser/laravel-countries 2.2.2

Laravel Countries 2.x

Quick Start

The essential Laravel Countries features with short examples for lookups, filtering, helpers, macros, validation and select options.

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

Country Code (ISO 3166-2)
Try: US, DE, JP, FR, CA, AU, BR, IN, CN, RU
πŸ‡ΊπŸ‡Έ

United States

US β€’ USA

Capital: Washington, D.C.
Region: americas
Calling Code: +1

Currency Information

Code: USD
Name: United States dollar
Symbol: $
Languages: en

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

Filter Method Select Region
52
Countries Found
πŸ‡¦πŸ‡©

Andorra

AD

Capital: Andorra la Vella
Currency: € EUR
πŸ‡¦πŸ‡±

Albania

AL

Capital: Tirana
Currency: L ALL
πŸ‡¦πŸ‡Ή

Austria

AT

Capital: Vienna
Currency: € EUR
πŸ‡¦πŸ‡½

Γ…land Islands

AX

Capital: Mariehamn
Currency: € EUR
πŸ‡§πŸ‡¦

Bosnia and Herzegovina

BA

Capital: Sarajevo
Currency: KM BAM
πŸ‡§πŸ‡ͺ

Belgium

BE

Capital: Brussels
Currency: € EUR
πŸ‡§πŸ‡¬

Bulgaria

BG

Capital: Sofia
Currency: Π»Π² BGN
πŸ‡§πŸ‡Ύ

Belarus

BY

Capital: Minsk
Currency: Br BYN
πŸ‡¨πŸ‡­

Switzerland

CH

Capital: Bern
Currency: Fr. CHF
πŸ‡¨πŸ‡Ύ

Cyprus

CY

Capital: Nicosia
Currency: € EUR
πŸ‡¨πŸ‡Ώ

Czechia

CZ

Capital: Prague
Currency: Kč CZK
πŸ‡©πŸ‡ͺ

Germany

DE

Capital: Berlin
Currency: € EUR
Showing first 12 of 52 results

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'); // true
country_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