Installation
composer require io238/laravel-iso-countries
Run migrations and seeders:
php artisan migrate --seed
First Use Case Fetch a country by ISO code (e.g., "US"):
use Io238\LaravelIsoCountries\Models\Country;
$usa = Country::where('iso_alpha2', 'US')->first();
echo $usa->name; // "United States"
Key Models
Country (ISO 3166-1 alpha-2)Language (ISO 639-1)Currency (ISO 4217)CountryLanguage (pivot table for country-language relationships)Multi-Language Support
Access localized names via localized():
$usa->localized('name', 'es'); // "Estados Unidos"
Country Lookups
Country::findByIsoAlpha2('US');
Country::findByIsoAlpha3('USA');
Country::findByIsoNumeric('840');
Country::whereLocalized('name', 'like', '%France%')->get();
Relationships
$canada = Country::find('CA');
$canada->languages; // Collection of Language models
$canada->currencies; // Collection of Currency models
$english = Language::where('iso_alpha2', 'en')->first();
$english->countries; // Countries where English is official
Data Augmentation Use traits in your models to auto-attach ISO data:
use Io238\LaravelIsoCountries\Traits\HasCountry;
use Io238\LaravelIsoCountries\Traits\HasLanguage;
class User extends Model {
use HasCountry, HasLanguage;
}
Then access via:
$user->country->name; // User's country name
Form Validation Validate ISO codes in Laravel:
use Io238\LaravelIsoCountries\Rules\ValidIsoAlpha2;
$request->validate([
'country_code' => ['required', new ValidIsoAlpha2],
]);
Localization Dynamically fetch translations for the current locale:
$country = Country::find('JP');
$country->name; // Auto-resolves to current app locale
return Country::find('DE')->append('localized_name')->toArray();
Country::pluck('name', 'iso_alpha2'); // ['US' => 'United States', ...]
lat/lon for maps:
$country = Country::find('AU');
['lat' => $country->lat, 'lng' => $country->lon];
use Io238\LaravelIsoCountries\Database\Seeders\IsoCountriesSeeder;
class CustomSeeder extends IsoCountriesSeeder {
public function run() {
$this->call([
// Override or add custom logic
]);
}
}
Locale Mismatches
app.locale in .env matches your expected language (e.g., en, es).localized():
$country->localized('name', 'fr'); // Override app locale
Database Conflicts
countries, languages, or currencies to the DB, avoid duplicate ISO codes (e.g., iso_alpha2 must be unique).upsert or disable the package’s seeder if managing data externally.Performance
Country::with(['languages', 'currencies'])->find('GB');
Cache::remember('all_countries', now()->addDays(7), function () {
return Country::all();
});
Deprecated Methods
getName() → name).Missing Data
iso_countries tables exist and are seeded:
php artisan db:seed --class=IsoCountriesSeeder
languages table not populated).Localization Issues
localized column exists in the countries/languages/currencies tables.php artisan migrate:fresh --seed
Relationship Errors
country->languages returns empty, check the country_language pivot table:
\DB::table('country_language')->count(); // Should return >0
Custom Fields
Add columns to the countries table (e.g., emoji):
Schema::table('countries', function (Blueprint $table) {
$table->string('emoji')->nullable();
});
Then update the seeder or manually populate.
Additional ISO Standards Extend the package by adding new models (e.g., ISO 3166-2 subdivisions):
// Example: Add a State model
class State extends Model {
public function country() {
return $this->belongsTo(Country::class);
}
}
Custom Localization
Override the localized() method in your model:
class Country extends \Io238\LaravelIsoCountries\Models\Country {
public function localized($attribute, $locale = null) {
// Custom logic (e.g., fallback to 'en' if locale missing)
}
}
Testing Use the package’s factories in tests:
use Io238\LaravelIsoCountries\Database\Factories\CountryFactory;
$country = CountryFactory::new()->create(['iso_alpha2' => 'XK']);
Country::where('region', 'EU')->update(['custom_flag' => true]);
class CountryService {
public function getCountryWithCache($isoCode) {
return Cache::remember("country_{$isoCode}", now()->addHours(1), function () use ($isoCode) {
return Country::findByIsoAlpha2($isoCode);
});
}
}
use Io238\LaravelIsoCountries\Rules\ValidIsoAlpha3;
$request->validate([
'country' => ['required', new ValidIsoAlpha3],
]);
How can I help you explore Laravel packages today?