league/iso3166
PHP library providing up-to-date ISO 3166-1 country data. Look up countries by name, alpha-2, alpha-3, or numeric code and get normalized details like country name, codes, and currency information.
composer require league/iso3166.League\ISO3166\ISO3166 class and use its lookup methods:
$iso = new League\ISO3166\ISO3166;
$france = $iso->alpha2('FR');
$germany = $iso->name('Germany');
alpha2(), alpha3(), numeric(), or name(). All return an associative array containing country data, or null if not found.exactName() if case- and diacritic-insensitive partial matches aren’t desired — useful for strict validation (added in v4.3.0).all() to retrieve all countries as an array, or count() to get the total number of entries.Form validation / user input normalization:
Normalize country inputs (e.g., from dropdowns or text fields) to alpha2/alpha3 codes. For example:
$countryCode = $iso->alpha2($request->get('country_name'));
Allow users to input names, then store only standardized codes in the database.
Lookup in domain entities:
Inject ISO3166 into services or domain models to enrich value objects (e.g., Address, Customer):
public function getCurrency(string $alpha2): ?string
{
$data = $this->iso3166->alpha2($alpha2);
return $data['currency'][0] ?? null;
}
Data exports & integrations: Enrich datasets (e.g., order shipments, CRM records) with ISO-compliant codes for reporting or external API integrations (e.g., tax engines, customs systems).
Static caching for performance:
Reuse the same instance or wrap it in a Laravel service provider for singleton usage. The class is stateless, but avoiding repeated new instances improves efficiency in loops.
Laravel integration patterns:
ISO3166 as a singleton in AppServiceProvider.new class implements Rule that checks iso->alpha2($value).name() uses case-insensitive partial match (v4.1.0+), so name('united') may return multiple results or unexpected matches (first match wins via internal iteration). Prefer exactName() for strict name lookups.currency key is always an array, even if only one currency exists (e.g., ['EUR']). Never assume scalar.XK) was added in v4.3.2; older versions omit it. If your use case depends on Kosovo, ensure you’re on ≥ v4.3.2.new ISO3166() calls; cache a single instance.?array — always guard against null or use null coalescing (?? []).How can I help you explore Laravel packages today?