arthurydalgo/laravel-iso-countries
Laravel package (fork) providing ISO 3166 countries, ISO 639 languages, and ISO 4217 currencies via ready-to-use models backed by a bundled SQLite database. Includes localized names and relationships (countries↔languages/currencies). Supports Laravel 11/12, PHP 8+.
Installation:
composer require arthurydalgo/laravel-iso-countries
No migrations are required—data is stored in a pre-built SQLite database.
Publish Config (Optional):
php artisan vendor:publish --provider="Io238\ISOCountries\ISOCountriesServiceProvider" --tag="config"
Default config includes en, de, fr, and es locales.
Rebuild Database (If Needed):
php artisan db:seed countries:build
Exclude database/iso-countries.sqlite from .gitignore to share across environments.
First Use Case: Fetch a country by ISO code:
use Io238\ISOCountries\Models\Country;
$country = Country::find('US'); // Returns Country model with localized names
Localized Data Access: Dynamically fetch names in the app’s locale:
app()->setLocale('fr');
Country::find('FR')->name; // Returns "France" in French
Relationship Queries: Leverage built-in relationships for multi-directional lookups:
// Countries using EUR
Currency::find('EUR')->countries;
// Languages spoken in Brazil
Country::find('BR')->languages;
Casting ISO Codes: Enrich existing models with metadata via custom casts:
use Io238\ISOCountries\Casts\Country;
class User {
protected $casts = ['country_code' => Country::class];
}
// Now `$user->country_code` returns a Country model.
Filtering by Region/Attributes: Use Eloquent queries with ISO data:
// Top 5 populated countries in Asia
Country::where('region', 'Asia')
->orderByDesc('population')
->limit(5)
->pluck('name');
Multi-Language Support: Extend translations by updating the config and rebuilding the database:
// config/iso-countries.php
'locales' => ['en', 'es', 'pt'],
Then run php artisan db:seed countries:build.
use Illuminate\Validation\Rule;
$request->validate([
'country' => ['required', Rule::exists('iso_countries', 'id')],
]);
return Country::find('CA')->only(['name', 'capital', 'currencies']);
flag-icon-css (included):
<img src="{{ asset('vendor/flag-icon-css/flags/1x1/{{ $country->id }}.svg') }}" alt="{{ $country->name }}">
Database Path:
iso-countries.sqlite from .gitignore can bloat repositories.!iso-countries.sqlite to .gitignore.Locale Fallback:
app.fallback_locale. Ensure this is set in config/app.php.Rebuilding Data:
countries:build) overwrites existing data. Backup if needed.Performance:
config/iso-countries.php. Rebuild the database if needed.id field in your models matches the ISO code (e.g., country_code = 'US').class Country extends \Io238\ISOCountries\Models\Country {
public function getFullNameAttribute() {
return $this->name . " (Capital: {$this->capital})";
}
}
Country::query()->macro('byRegion', function ($region) {
return $this->where('region', $region);
});
$euroCountries = Cache::remember('euro_countries', now()->addDays(7), function () {
return Currency::find('EUR')->countries->pluck('name');
});
public function before($user, $ability) {
if ($user->country_code === 'US') {
return true;
}
}
$this->mock(Io238\ISOCountries\Models\Country::class, function ($mock) {
$mock->shouldReceive('find')->andReturn(new Country(['id' => 'US']));
});
How can I help you explore Laravel packages today?