Installation:
composer require drnd-dev/laravel-countries
php artisan vendor:publish --provider="DrndDev\Countries\CountriesServiceProvider"
First Use Case:
Fetch a country by ISO code (e.g., US):
use DrndDev\Countries\Facades\Countries;
$country = Countries::get('US');
// Returns: Country object with name, code, translations, coordinates, etc.
Where to Look First:
DrndDev\Countries\Facades\Countries (primary entry point).config/countries.php (e.g., default locale, data source).Fetching Countries:
$country = Countries::get('GB'); // By ISO code
$country = Countries::find(826); // By numeric code
$countries = Countries::all(); // Collection of Country objects
$countries = Countries::list(); // Array of [code => name] pairs
$europeanCountries = Countries::where('region', 'EU')->get();
Multilingual Support:
$country->name('es'); // Spanish name
$country->translations; // All available translations
config/countries.php:
'default_locale' => 'en',
Geographic Data:
$latitude = $country->latitude;
$region = $country->region; // e.g., 'Americas'
Integration with Eloquent:
use DrndDev\Countries\Database\Traits\HasCountries;
class User extends Model
{
use HasCountries;
}
// Automatically adds `country_id` column and accessors.
Caching:
'cache' => true,
php artisan countries:clear-cache
Custom Data Sources:
Countries::setDataSource(app_path('data/custom_countries.json'));
API Responses:
$response = Countries::get('CA')->toArray();
// Returns: ['code' => 'CA', 'name' => 'Canada', ...]
Validation:
use DrndDev\Countries\Rules\ValidCountry;
$request->validate([
'country' => ['required', new ValidCountry],
]);
Events:
CountryRetrieved):
Countries::get('JP'); // Triggers events
Locale Mismatches:
config/countries.php:
'supported_locales' => ['en', 'es', 'fr'],
Caching Issues:
php artisan countries:clear-cache
Database vs. In-Memory:
CountryRepository interface.Numeric vs. ISO Codes:
826 for Canada), while others use ISO (CA). Double-check the expected format in the method signature.Migration Conflicts:
countries tables in your app.Log Country Data:
\Log::info('Country data:', ['data' => Countries::get('DE')->toArray()]);
Check Data Source:
config/countries.php:
'data_source' => database_path('countries.json'),
Validate ISO Codes:
isValidIsoCode() helper:
if (!Countries::isValidIsoCode('XX')) {
// Handle invalid code
}
Custom Fields:
Country model to add custom attributes:
class CustomCountry extends \DrndDev\Countries\Models\Country
{
protected $appends = ['custom_field'];
}
Override Data:
$this->app->bind(
\DrndDev\Countries\Contracts\CountryRepository::class,
\App\Repositories\CustomCountryRepository::class
);
Add New Locales:
Country model to support additional translations:
public function name($locale = null)
{
$locale = $locale ?: config('countries.default_locale');
return $this->translations[$locale] ?? parent::name($locale);
}
Testing:
Countries facade in tests:
$this->mock(Countries::class)->shouldReceive('get')->andReturn($mockCountry);
How can I help you explore Laravel packages today?