stidges/country-flags
stidges/country-flags is a lightweight Laravel package that turns ISO country codes into flag emoji or image paths. It provides simple helpers and Blade directives for displaying flags anywhere in your app, with no database or extra configuration required.
Install with Composer:
composer require stidges/country-flags
The package is Laravel-auto-discovered on 5.5+, so no manual service-provider registration is usually needed. On older apps, add the provider/facade alias from the package README to config/app.php.
Look first at:
Stidges\CountryFlags\CountryFlag) — it exposes the conversion method, usually get().First use case: render a flag next to a user’s country.
use Stidges\CountryFlags\CountryFlag;
CountryFlag::get('US'); // 🇺🇸
CountryFlag::get('be'); // 🇧🇪 (verify case handling in your version)
In a Blade view:
{{ CountryFlag::get($user->country_code) }} {{ $user->name }}
If the package registers a helper, the same result is often available as country_flag('US'). Check the README/provider before relying on it — helper registration varies by release.
Model accessor — keep flag rendering out of views:
public function getFlagAttribute(): string
{
return CountryFlag::get($this->country_code) ?? '';
}
Then {{ $user->flag }} in Blade. Add $appends = ['flag'] only if you actually need it in JSON.
API resources — return the emoji as a separate field rather than embedding it in a name:
return [
'country_code' => $this->country_code,
'flag' => CountryFlag::get($this->country_code),
];
Blade component for consistent markup and accessibility:
{{-- resources/views/components/flag.blade.php --}}
@props(['code'])
<span role="img" aria-label="{{ strtoupper($code) }} flag">
{{ CountryFlag::get($code) }}
</span>
Usage: <x-flag code="{{ $user->country_code }}" />.
Fallback chain — country codes are often nullable or dirty:
$flag = CountryFlag::get($code)
?? CountryFlag::get(strtoupper($code))
?? '🏳️';
Form validation — pair the package with a validation rule so invalid codes don’t silently produce null:
'country_code' => ['required', 'string', 'size:2', 'alpha'],
Select lists — combine with a country-name package (or your own mapping) for <option> labels, and use this package only for the emoji preview.
Caching — if you render large lists, resolve flags once and cache the resulting HTML/array; the conversion itself is cheap, but per-row facade calls across thousands of rows add up.
Emails and notifications — flags render fine in HTML mail if the client supports emoji fonts; always include the country name as text for clients that don’t.
US, GB). USA or United States will not work. Normalize codes at the boundary (form request, importer) rather than scattering strtoupper() calls.null or the input unchanged, depending on version. Test the exact behavior for your release and always provide a fallback; don’t assume a string is returned.strtoupper() before calling, or wrap the call in a small helper you control.<span> with role="img" and an aria-label containing the country name.php artisan config:clear during development — a cached config will ignore your changes.country_code field, or assert against "\u{1F1FA}\u{1F1F8}"-style Unicode escapes rather than pasting the literal flag into the test file.setFlags()-style method), override entries there for non-standard codes (e.g. XK for Kosovo) instead of forking. Verify the key format matches what get() expects.How can I help you explore Laravel packages today?