jszdavid/laravel-world
Laravel package providing countries, states, and cities data (based on nnjeim/world) with migrations, seeder, configurable table names and route prefix, optional country filters, and a World facade/API endpoints with optional query caching.
Installation:
composer require jszd2022/laravel-world
php artisan vendor:publish --provider="JSzD\World\WorldServiceProvider"
php artisan migrate
php -d memory_limit=512M artisan db:seed --class=WorldSeeder
config/laravel-world.php file exists after publishing.First Use Case: Fetch all countries with the facade:
use JSzD\World\Facades\World;
$countries = World::countries()->get();
Country models with default fields (id, name, iso2).Where to Look First:
JSzD\World\Facades\World (primary entry point).config/laravel-world.php (customize table names, routes, or country filters).database/migrations/[timestamp]_create_laravel_world_tables.php (schema reference).Data Retrieval:
// Basic fetch
$countries = World::countries()->get();
// Filtered (e.g., by region)
$americanCountries = World::countries()->where('region', 'Americas')->get();
// Select specific fields
$countries = World::countries()->select('name', 'iso2')->get();
$states = World::states()->ofCountry(1)->get(); // 1 = country ID
$cities = World::cities()->ofState(1)->get(); // 1 = state ID
$country = World::countries()->with('states.cities')->find(1);
API Routes (if enabled):
config('laravel-world.routes.prefix'):
GET /api/countries → All countries.GET /api/countries/{id}/states → States for a country.GET /api/states/{id}/cities → Cities for a state.routes/api.php or disabling in config.Caching:
config('laravel-world.cache_ttl') (default: 1 week).World::clearCache();
Filtering Countries During Seed:
config('laravel-world.countries.only') or except to limit seeded data:
// config/laravel-world.php
'countries' => [
'only' => ['US', 'CA', 'GB'], // Seed only these countries
],
php artisan db:seed --class=WorldSeeder after changes.Eloquent Relationships: Define relationships in your models to leverage Laravel’s ORM:
// app/Models/Country.php
public function states()
{
return $this->hasMany(State::class, 'country_id');
}
country_id foreign keys.Validation: Use the package for form validation (e.g., country/state/city selection):
use JSzD\World\Rules\ValidCountry;
$request->validate([
'country' => ['required', new ValidCountry],
]);
Localization:
Extend the name field to support translations:
// Add to migration
Schema::table('laravel-world-countries', function (Blueprint $table) {
$table->string('name_en')->nullable();
$table->string('name_es')->nullable();
});
Testing:
World::fake() for unit tests:
World::fake([
'countries' => ['US' => ['name' => 'United States']],
]);
Memory Limits:
memory_limit in php.ini if needed:
memory_limit = 1G
only/except filters.Table Name Conflicts:
laravel-world-*) may clash with existing tables.'migrations' => [
'countries' => ['table_name' => 'custom_countries'],
],
php artisan migrate:fresh if tables already exist.API Route Overrides:
'routes.enabled' => false) does not remove the provider’s route registration.routes/api.php or use middleware to block access.Cache Invalidation:
cache_ttl to 0 for development:
config(['laravel-world.cache_ttl' => 0]);
ISO Codes:
US, GB). Validate inputs to avoid mismatches:
$validCodes = World::countries()->pluck('iso2');
Seeder Issues:
SELECT * FROM laravel-world-states WHERE country_id NOT IN (SELECT id FROM laravel-world-countries);
php artisan db:seed --class=WorldSeeder --force
Performance:
select() to limit fields:
World::countries()->select('id', 'name', 'iso2')->get();
with() on large datasets (e.g., all cities for a country).Configuration Overrides:
php artisan vendor:publish --provider="JSzD\World\WorldServiceProvider" --tag=config
Extension Points:
population, flag).php artisan vendor:publish --provider="JSzD\World\WorldServiceProvider" --tag=models
app/Models/World/Country.php, etc.Logging:
'debug' => env('APP_DEBUG', false),
tail -f storage/logs/laravel.log
Partial Seeding:
php artisan db:seed --class=WorldSeeder --only=US,CA
Dynamic Data:
World::countries()->uncached()->get();
Geocoding Integration:
spatie/laravel-geocoder for location-based features:
$city = World::cities()->find(1);
$coordinates = geoCode($city->name);
Soft Deletes:
$table->softDeletes();
withTrashed() to include "deleted" records.How can I help you explore Laravel packages today?