Installation:
composer require webpatser/laravel-countries
php artisan countries:install
install command publishes the database table and migrations (if using the Eloquent model feature).Basic Usage:
use Webpatser\Countries\Countries;
$countries = new Countries();
Countries facade or class in your controller/service.First Use Case: Fetch a country by ISO code (e.g., for a user profile form):
$country = Countries::getOne('US');
return view('profile', ['country' => $country]);
Countries::getOne(), Countries::getList(), Countries::getByCurrency().use Webpatser\Countries\Rules\Country; for form validation.countryName()).Country Selection in Forms:
// Blade template
<select name="country">
@foreach (Countries::getList() as $country)
<option value="{{ $country->iso2 }}">{{ $country->name }}</option>
@endforeach
</select>
Countries::getList() to populate dropdowns dynamically.Filtering Countries by Region/Currency:
// Get all EU countries
$euCountries = Countries::getByRegion('EU');
// Get countries using EUR
$eurCountries = Countries::getByCurrency('EUR');
Validation:
use Webpatser\Countries\Rules\Country;
$request->validate([
'country' => ['required', new Country],
]);
US, DE) or country names.Eloquent Model Integration:
use Webpatser\Countries\HasCountries;
class User extends Model
{
use HasCountries;
}
user->country returns a Country object).Collection Macros:
// Add a macro to get country names from a collection of users
Collect::macro('countryNames', function () {
return $this->pluck('country')->map(fn ($country) => $country->name);
});
// Usage
$names = User::all()->countryNames();
Countries::getList() if performance is critical (e.g., in a high-traffic app):
$countries = Cache::remember('all-countries', now()->addDays(7), function () {
return Countries::getList();
});
resources/lang/en/countries.php).return response()->json([
'country' => Countries::getOne($request->country)->toArray(),
]);
Database vs. In-Memory:
HasCountries), run migrations after countries:install:
php artisan migrate
HasCountries to fail silently.ISO Code Case Sensitivity:
US, us) are case-sensitive in Countries::getOne(). Always use uppercase:
// Correct
$usa = Countries::getOne('US');
// Fails silently (returns null)
$usa = Countries::getOne('us');
Collection Macros Overwriting:
country() or name(), it may conflict with existing methods. Use unique names:
Collect::macro('countryNames', function () { ... });
Flag Emoji Rendering:
countries table exists (if using Eloquent) or check the in-memory data structure:
dd(Countries::getList()->first()->toArray());
Country rule is imported correctly. Common typo: use Webpatser\Countries\Rule\Country; (missing s).Custom Fields:
Country model to add custom attributes:
class Country extends \Webpatser\Countries\Country
{
public function getPhoneCodeAttribute()
{
return $this->attributes['phone_code'] ?? '+1'; // Example
}
}
Countries service provider to modify the model.Additional Data Sources:
$countries = Countries::getList()->merge($externalApiData);
Testing:
Countries facade in tests:
$this->mock(Countries::class, function ($mock) {
$mock->shouldReceive('getOne')->andReturn(new Country(['iso2' => 'US']));
});
Performance:
$countries->getList()->load('regions', 'currency');
with() on the underlying query if using Eloquent.Published Config:
php artisan vendor:publish --provider="Webpatser\Countries\CountriesServiceProvider"
config/countries.php.Language Fallback:
app/Providers/AppServiceProvider:
Countries::setFallbackLanguage('es');
How can I help you explore Laravel packages today?