Quick Start Guide
Learn the essential features and common usage patterns for Laravel Countries.
Basic Usage
The Countries class is the main entry point for accessing country data:
use Webpatser\Countries\Countries; $countries = new Countries(); // Get a single country$usa = $countries->getOne('US');echo $usa['name']; // "United States"echo $usa['flag']; // "πΊπΈ"echo $usa['capital']; // "Washington D.C." // Get all countries$allCountries = $countries->getList();echo count($allCountries); // 249
πΊπΈ
United States of America
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 of America" echo $country['flag']; // "πΊπΈ" echo $country['capital']; // "Washington, D.C."
Filtering and Searching
Filter by Currency
// Get all countries using Euro$euroCountries = $countries->getByCurrency('EUR'); foreach ($euroCountries as $code => $country) { echo $country['flag'] . ' ' . $country['name'] . PHP_EOL;}// π¦π© Principality of Andorra// π¦πΉ Republic of Austria// π§πͺ Kingdom of Belgium// ...
Filter by Region
// Get all European countries$europeanCountries = $countries->getByRegion('Europe'); // Get all Asian countries$asianCountries = $countries->getByRegion('Asia');
Search Countries
// Search by name or capital$searchResults = $countries->search('united'); // Results include:// - United States// - United Kingdom// - United Arab Emirates
52
Countries Found
π¦π©
Principality of Andorra
AD
Capital:
Andorra la Vella
Currency:
β¬ EUR
π¦π±
Republic of Albania
AL
Capital:
Tirana
Currency:
L ALL
π¦πΉ
Republic of Austria
AT
Capital:
Vienna
Currency:
β¬ EUR
π¦π½
Γ land Islands
AX
Capital:
Mariehamn
Currency:
β¬ EUR
π§π¦
Bosnia and Herzegovina
BA
Capital:
Sarajevo
Currency:
KM BAM
π§πͺ
Kingdom of Belgium
BE
Capital:
Brussels
Currency:
β¬ EUR
π§π¬
Republic of Bulgaria
BG
Capital:
Sofia
Currency:
Π»Π² BGN
π§πΎ
Republic of Belarus
BY
Capital:
Minsk
Currency:
Br BYN
π¨π
Swiss Confederation
CH
Capital:
Bern
Currency:
Fr. CHF
π¨πΎ
Republic of Cyprus
CY
Capital:
Nicosia
Currency:
β¬ EUR
π¨πΏ
Czech Republic
CZ
Capital:
Prague
Currency:
KΔ CZK
π©πͺ
Federal Republic of 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
Laravel Countries provides convenient global helper functions:
// Get country informationcountry_name('US'); // "United States"country_flag('US'); // "πΊπΈ"country_capital('US'); // "Washington D.C."country_formatted('US'); // "πΊπΈ United States" // Get currency information$currency = country_currency('US');// ['code' => 'USD', 'name' => 'US Dollar', 'symbol' => '$'] // Check if country existscountry_exists('US'); // truecountry_exists('ZZ'); // false // Convert between formatscountry_code_to_flag('DE'); // "π©πͺ"flag_to_country_code('π―π΅'); // "JP"
Collection Macros
Extend Laravel Collections with country-specific methods:
use Webpatser\Countries\Countries; $countries = collect((new Countries())->getList()); // Filter by region$europeanCountries = $countries->byRegion('Europe'); // Filter by currency$euroCountries = $countries->byCurrency('EUR'); // Search countries$searchResults = $countries->searchCountries('united'); // Get just country names$countryNames = $countries->countryNames(); // Get just flags$flags = $countries->countryFlags(); // Chain multiple operations$result = $countries ->byRegion('Europe') ->byCurrency('EUR') ->countryNames() ->sort();
String Macros
Extend Laravel's Str class with country utilities:
use Illuminate\Support\Str; // Convert country codes to flagsStr::toCountryFlag('US'); // "πΊπΈ"Str::toCountryFlag('DE'); // "π©πͺ" // Convert flags back to codesStr::fromCountryFlag('πΊπΈ'); // "US"Str::fromCountryFlag('π©πͺ'); // "DE" // Get country namesStr::countryName('FR'); // "French Republic"Str::countryName('JP'); // "Japan"
Form Validation
Use built-in validation rules for forms:
use Webpatser\Countries\Rules\ValidCountryCode;use Webpatser\Countries\Rules\ValidCurrencyCode;use Webpatser\Countries\Rules\ValidRegion; // In your form request or controller$request->validate([ 'country' => ['required', new ValidCountryCode('iso_3166_2')], 'currency' => ['nullable', new ValidCurrencyCode()], 'region' => ['nullable', new ValidRegion()],]);
Common Use Cases
Country Dropdown for Forms
// Get select options for forms$countryOptions = countries_select_options(); // With flags$countryOptions = countries_select_options(null, true); // Only European countries$europeanOptions = countries_select_options('Europe');
User Profile with Country
use Webpatser\Countries\Traits\HasCountry; class User extends Model{ use HasCountry; // country_code field in database} // Now you can use:$user->country->name; // Country name$user->country_name; // Via accessor$user->formatted_country; // "πΊπΈ United States"$user->isFromEurope(); // Boolean check
Analytics by Region
// Get users grouped by region$usersByRegion = User::all() ->groupBy(fn($user) => country_region($user->country_code)) ->map->count(); // Get revenue by currency$revenueByCurrency = Order::all() ->groupBy(fn($order) => country_currency($order->country)['code']) ->map(fn($orders) => $orders->sum('total'));
Performance Tips
Caching
Enable caching for JSON source or frequent lookups:
// In config/countries.php'cache_ttl' => 3600, // Cache for 1 hour
Eloquent Optimization
// Eager load countries for multiple users$users = User::with('country')->get(); // Use specific selects to reduce memory$countries = Country::select('iso_3166_2', 'name', 'flag')->get();