Installation:
composer require pragmarx/countries
No additional configuration is required—just autoload the package.
First Use Case: Fetch all countries with their ISO codes and names:
use Pragmarx\Countries\Countries;
$countries = Countries::all();
// Returns a collection of Country objects with properties like `name`, `iso2`, `iso3`, etc.
Where to Look First:
all(), get(), where(), etc.).Country class structure (e.g., $country->iso2, $country->currencies).Fetching Data:
$countries = Countries::all(); // Collection of Country objects
$country = Countries::get('US'); // Returns a Country object for the US
$europeanCountries = Countries::where('continent', 'Europe')->get();
Integration with Laravel:
// In AppServiceProvider@boot()
$this->app->singleton('countries', function () {
return new \Pragmarx\Countries\Countries();
});
Then inject via constructor or resolve:
$countries = app('countries')->get('BR');
Common Use Cases:
$options = Countries::all()->pluck('name', 'iso2')->toArray();
use Illuminate\Validation\Rule;
$validator->addRules([
'country_code' => ['required', Rule::in(Countries::all()->pluck('iso2')->toArray())],
]);
$country = Countries::get('JP');
$locale = $country->locale; // e.g., 'ja_JP'
Lazy Loading:
Use get() for single countries or where() for filtered subsets to avoid loading all data unnecessarily.
Case Sensitivity:
'US', 'us') are case-sensitive in get(). Always use uppercase:
$country = Countries::get('US'); // Correct
$country = Countries::get('us'); // Returns null
Data Mutability:
Country objects are immutable by default. Avoid modifying properties directly (e.g., $country->name = 'New Name'). Use methods like setName() if available (check the API).Memory Usage:
Countries::all() loads all 250+ countries into memory. For large applications, cache the result:
$countries = Cache::remember('all_countries', now()->addDays(30), function () {
return Countries::all();
});
Deprecated Methods:
Countries::iso() or Countries::name(). Prefer Countries::get('ISO') for clarity.Verify ISO Codes:
Countries::all()->pluck('iso2') to list all valid codes before debugging missing data.Check for Updates:
composer update pragmarx/countries if you encounter missing countries/currencies.Custom Data:
Country class or use traits to add custom properties:
class ExtendedCountry extends \Pragmarx\Countries\Country {
public function getPhoneCode() {
return $this->phone_code ?? 'N/A';
}
}
Then override the factory or use composition.Add Custom Fields:
Country class or use a wrapper:
class CustomCountry {
public function __construct(private \Pragmarx\Countries\Country $country) {}
public function getRegion() {
return $this->country->subregion ?? 'N/A';
}
}
Local Overrides:
countries.php) in vendor/pragmarx/countries/src/Data/ (not recommended for production; use a fork instead).Testing:
Countries class in tests:
$mockCountries = Mockery::mock(\Pragmarx\Countries\Countries::class);
$mockCountries->shouldReceive('get')->with('BR')->andReturn(new Country(['iso2' => 'BR']));
$this->app->instance(\Pragmarx\Countries\Countries::class, $mockCountries);
Performance:
// One-time setup
Countries::all()->each(function ($country) {
\App\Models\Country::updateOrCreate(
['iso2' => $country->iso2],
['name' => $country->name]
);
});
How can I help you explore Laravel packages today?