src/helpers.php defines global functions that wrap Webpatser\Countries\Helpers\CountryHelper, which in turn reads from the Countries class. Each function is declared only if the name is not already taken. Country codes are case-insensitive; functions that look up one country return null for an unknown code.
Available Helper Functions
| Function | Returns |
|---|---|
country_name($code) |
Common name, ?string |
country_flag($code) |
Flag emoji, ?string |
country_capital($code) |
Capital, ?string |
country_currency($code) |
['code', 'name', 'symbol'], ?array |
country_region($code) |
Region, ?string |
country_languages($code) |
Language codes, ?array |
country_calling_code($code) |
Calling code with a leading +, ?string |
country_formatted($code, $includeFlag = true) |
Flag and name, ?string |
country_exists($code) |
bool |
country_code_from_name($name) |
Code for a common or official name, ?string |
countries_by_region($region) |
Array keyed by code |
countries_by_currency($currency) |
Array keyed by code |
countries_search($query) |
Array keyed by code |
countries_select_options($region = null, $includeFlags = true) |
[code => label] sorted by name |
country_code_to_flag($code) |
Flag emoji, ?string |
flag_to_country_code($flag) |
Code, ?string |
Basic Information Functions
country_name($code)
Returns the common name. The official name is in the full_name field of Countries::getOne().
echo country_name('US'); // "United States"echo country_name('DE'); // "Germany"echo country_name('JP'); // "Japan"var_dump(country_name('XYZ')); // NULL
country_flag($code)
echo country_flag('US'); // "πΊπΈ"echo country_flag('DE'); // "π©πͺ"var_dump(country_flag('XYZ')); // NULL
country_capital($code)
echo country_capital('US'); // "Washington, D.C."echo country_capital('DE'); // "Berlin"echo country_capital('JP'); // "Tokyo"
country_formatted($code, $includeFlag = true)
echo country_formatted('US'); // "πΊπΈ United States"echo country_formatted('DE'); // "π©πͺ Germany"echo country_formatted('JP', false); // "Japan"
country_calling_code($code)
echo country_calling_code('US'); // "+1"echo country_calling_code('DE'); // "+49"
Currency Functions
country_currency($code)
$currency = country_currency('US');// ['code' => 'USD', 'name' => 'United States dollar', 'symbol' => '$'] $euroCurrency = country_currency('DE');// ['code' => 'EUR', 'name' => 'Euro', 'symbol' => 'β¬']
Usage in Templates
@php $currency = country_currency($user->country_code) @endphp <span>{{ $currency['symbol'] }}{{ $price }}</span><small>{{ $currency['name'] }}</small>
Utility Functions
country_exists($code)
if (country_exists('US')) { echo "Valid country code";} $isValid = country_exists($request->country_code);
country_region($code)
Regions in the data are lowercase (africa, americas, asia, europe, oceania).
echo country_region('US'); // "americas"echo country_region('DE'); // "europe"echo country_region('JP'); // "asia"
country_languages($code)
Returns language codes:
country_languages('US'); // ['en']country_languages('CH'); // ['de', 'fr', 'it', 'rm']
country_code_from_name($name)
Matches the common name or the official name, case-insensitive:
country_code_from_name('Greece'); // "GR"country_code_from_name('Hellenic Republic'); // "GR"country_code_from_name('Atlantis'); // null
Collection Functions
$europeanCountries = countries_by_region('Europe');$euroCountries = countries_by_currency('EUR');$results = countries_search('united'); // name, official name or capital // Each result is keyed by country code
countries_select_options($region, $includeFlags)
$options = countries_select_options(); // all countries, with flags$europeanOptions = countries_select_options('Europe'); // Europe only$plain = countries_select_options(null, false); // no flags
<select name="country"> @foreach(countries_select_options('Europe', true) as $code => $display) <option value="{{ $code }}">{{ $display }}</option> @endforeach</select>
Flag Conversion Functions
Both functions work on the two-letter pattern only; they do not check that the country exists.
echo country_code_to_flag('US'); // "πΊπΈ"echo country_code_to_flag('DE'); // "π©πͺ" echo flag_to_country_code('πΊπΈ'); // "US"echo flag_to_country_code('π―π΅'); // "JP"var_dump(flag_to_country_code('x')); // NULL
Real-world Examples
User Profile Display
<div class="user-profile"> <h3>{{ $user->name }}</h3> <p> <strong>Location:</strong> {{ country_formatted($user->country_code) }} </p> <p> <strong>Capital:</strong> {{ country_capital($user->country_code) }} </p></div>
E-commerce Price Display
public function showProduct($id, Request $request){ $product = Product::find($id); $userCountry = $request->header('CF-IPCountry', 'US'); $currency = country_currency($userCountry); return view('product', compact('product', 'currency'));}
API Response Formatting
return response()->json([ 'user' => [ 'name' => $user->name, 'email' => $user->email, 'country' => [ 'code' => $user->country_code, 'name' => country_name($user->country_code), 'flag' => country_flag($user->country_code), 'region' => country_region($user->country_code), ], ],]);
Form Validation with Helpers
public function rules(){ return [ 'country_code' => [ 'required', function ($attribute, $value, $fail) { if (! country_exists($value)) { $fail('The selected country is invalid.'); } }, ], ];}
For this check, the ValidCountryCode rule does the same job, see Validation Rules.
Notes on Data Loading
The helpers share one static Countries instance (separate from the container singleton) for the lifetime of the PHP process; under Octane or queue workers it persists across requests. That instance loads the list once, through the cache when cache_ttl is above 0. Each call after the first reads from memory.
Next Steps
- API Reference: Every class, method and function.
- Quick Start: Review the basics and common usage patterns.