aheenam/countries
Laravel wrapper around umpirsky/country-list providing simple country name lookups. Retrieve all countries or a specific country by code, in the current locale or another language via allIn() and get(). Lightweight fallback to antonioribeiro/countries.
Installation:
composer require aheenam/countries
Publish the configuration (if needed):
php artisan vendor:publish --provider="Aheenam\Countries\CountriesServiceProvider"
First Use Case: Fetch all countries as an array:
use Aheenam\Countries\Facades\Countries;
$countries = Countries::all();
// Returns an array of country data (name, code, etc.)
Where to Look First:
Aheenam\Countries\Facades\Countries (primary entry point).config/countries.php (if published).vendor/aheenam/countries/src/Countries/Countries.php for raw data structure.Fetching Countries:
// Get all countries
$allCountries = Countries::all();
// Get a single country by code
$country = Countries::get('US');
// Get countries by region (if supported)
$europeanCountries = Countries::where('region', 'Europe')->get();
Integration with Forms/Validation:
use Illuminate\Support\Facades\Validator;
$validator = Validator::make($request->all(), [
'country_code' => 'required|country_code', // Uses package's validation rule
]);
Localization:
// Override country names (e.g., for translations)
Countries::setNames([
'US' => 'United States of America (Custom)',
'GB' => 'United Kingdom (Custom)',
]);
Dynamic Data Handling:
// Filter countries by flag availability
$countriesWithFlags = collect(Countries::all())
->where('flag', '!=', null)
->values()
->all();
Eloquent Relationships:
// Add to a User model
public function country()
{
return $this->belongsTo(Country::class, 'country_code', 'code');
}
(Note: Requires a Country model; package doesn’t include one—you’ll need to create it.)
Blade Templates:
@foreach(Countries::all() as $country)
<option value="{{ $country['code'] }}">
{{ $country['name'] }}
</option>
@endforeach
API Responses:
return response()->json([
'countries' => Countries::all(),
]);
Outdated Data:
Countries::setNames() or extend the package.No Eloquent Model:
Country model/seed it manually:
// Example seeder
foreach (Countries::all() as $country) {
Country::updateOrCreate(
['code' => $country['code']],
$country
);
}
Validation Rule Assumption:
country_code validation rule assumes ISO 3166-1 alpha-2 codes. Test edge cases (e.g., XX for "unknown").Configuration Overrides:
config/countries.php exists before assuming defaults.Data Structure:
Inspect the raw output of Countries::all() to confirm fields (e.g., name, code, flag, region).
Example output:
[
'US' => ['name' => 'United States', 'code' => 'US', 'flag' => '🇺🇸'],
'GB' => ['name' => 'United Kingdom', 'code' => 'GB', 'flag' => '🇬🇧'],
]
Missing Countries:
If a country is missing, check if it exists in the package’s source data (vendor/aheenam/countries/src/Countries/Countries.php).
Custom Data Sources:
Extend the Aheenam\Countries\Countries class to load from a database or external API:
class CustomCountries extends Countries {
public function all() {
return Cache::remember('custom_countries', now()->addDays(1), function () {
return DB::table('countries')->get()->keyBy('code');
});
}
}
Bind it in AppServiceProvider:
$this->app->bind('Aheenam\Countries\Countries', function ($app) {
return new CustomCountries();
});
Adding Fields:
Merge additional data (e.g., phone_code) into the country array:
$countries = Countries::all();
foreach ($countries as &$country) {
$country['phone_code'] = $this->getPhoneCode($country['code']);
}
Testing: Mock the facade in tests:
Countries::shouldReceive('all')->andReturn([...]);
How can I help you explore Laravel packages today?