Installation:
composer require sewidan/world
php artisan vendor:publish --tag=world
php artisan migrate
First Use Case: Fetch all countries via the Facade:
use Sewidan\World\Facades\World;
$countries = World::countries();
Country models with nested states, cities, and currencies.Quick API Access:
The package includes API routes (e.g., /api/countries). Test with:
php artisan route:list | grep world
Data Retrieval:
// Get a country by ISO code
$country = World::country('US');
// Get states for a country
$states = World::states('US');
// Get cities for a state (e.g., California)
$cities = World::cities('US', 'CA');
with() to avoid N+1 queries:
$country = World::country('US')->load('states.cities');
Search Functionality (v1.1.12+):
// Search countries by name
$results = World::search('United', 'country');
// Search cities in a country
$results = World::search('New York', 'city', ['country_id' => 'US']);
API Integration:
// Fetch countries via AJAX
fetch('/api/countries')
.then(response => response.json())
.then(data => console.log(data));
Custom Queries:
use Sewidan\World\Models\Country;
$countries = Country::where('name', 'like', '%United%')->get();
Form Validation: Integrate with Laravel Validation:
use Sewidan\World\Rules\ValidCountry;
$request->validate([
'country_code' => ['required', new ValidCountry],
]);
Localization:
// app/Database/Seeders/CustomWorldSeeder.php
public function run()
{
$this->call([
\Sewidan\World\Database\Seeders\CountriesSeeder::class,
\App\Database\Seeders\CustomCountryNamesSeeder::class, // Your custom logic
]);
}
Caching: Cache frequent queries (e.g., country lists):
$countries = Cache::remember('world.countries', now()->addHours(1), function () {
return World::countries();
});
Partial Seeding:
php artisan install:country US # Only seed USA data
Migration Conflicts:
php artisan vendor:publish --tag=world --force
php artisan migrate:fresh
sub_region field was renamed to subregion in v1.1.12. Refresh migrations if needed.Performance with Large Datasets:
$cities = World::cities('US', 'CA')->paginate(20);
API Route Conflicts:
/api/world/*. Ensure no naming clashes with your existing routes.Seeder Overwrites:
php artisan db:seed --class=WorldSeeder drops and recreates all tables. Use --force cautiously in production.Check Config:
config/world.php for:
allowed_countries/disallowed_countries (restricts seeding).countries_table).Log Seeder Issues:
app/Database/Seeders/WorldSeeder.php if seeding fails:
\Log::info('Seeding countries...', ['count' => Country::count()]);
Validate Data:
php artisan tinker
>>> \Sewidan\World\Models\Country::where('iso', 'US')->first();
Custom Fields:
Country, State, or City models in app/Models:
namespace App\Models;
use Sewidan\World\Models\Country as BaseCountry;
class Country extends BaseCountry
{
protected $appends = ['custom_field'];
}
Add New Regions:
// app/Database/Seeders/CustomRegionsSeeder.php
public function run()
{
DB::table('regions')->insert([
['country_id' => 1, 'name' => 'District A', 'type' => 'district'],
]);
}
Override API Responses:
php artisan vendor:publish --tag=world-api
app/Http/Controllers/World/CountryController.php to customize responses.Timezones:
$timezones = World::timezones('US'); // Get timezones for a country
How can I help you explore Laravel packages today?