Installation:
composer require tocaan/world
php artisan vendor:publish --tag=world
php artisan migrate
php artisan db:seed --class=WorldSeeder
world config file in config/world.php and the published migrations/seeder in database/migrations/.First Use Case:
use Tocaan\World\Facades\World;
$countries = World::countries();
GET /api/countries
config/world.php (customize table names, optional fields, or restrict countries via allowed_countries/disallowed_countries).Tocaan\World\Facades\World (primary interface for data retrieval)./api/countries, /api/states/{country_code}, etc. (documented in the package).database/seeders/WorldSeeder.php (understand the data structure before querying).Data Retrieval via Facade:
$states = World::states('US'); // States for the USA
$cities = World::cities('US', 'CA'); // Cities in California
search argument (v1.1.12+):
$countries = World::countries()->search('France');
API Integration:
// Fetch countries via JavaScript
fetch('/api/countries')
.then(response => response.json())
.then(data => console.log(data));
WorldController (published with vendor:publish).Database-Driven Customization:
config/world.php:
'allowed_countries' => ['US', 'CA', 'GB'], // Only seed these countries
'disallowed_countries' => ['RU', 'CN'], // Exclude these
subregion or currency_code in the config.Eager Loading:
$countries = World::countries()->with(['currencies', 'timezones'])->get();
Country, State, etc., models (extend the published models if needed).use Tocaan\World\Facades\World;
$validator = Validator::make($request->all(), [
'state' => ['required', function ($attribute, $value, $fail) use ($request) {
$countryCode = $request->country;
if (!World::states($countryCode)->contains('name', $value)) {
$fail('The selected state is invalid for the chosen country.');
}
}],
]);
$countries = Cache::remember('world.countries', now()->addHours(1), function () {
return World::countries()->get();
});
Migration/Seeder Conflicts:
php artisan vendor:publish --tag=world --force to avoid route/config conflicts.sub_region field was renamed to subregion in v1.1.12. If you’ve customized migrations, update them or refresh the database.Case Sensitivity:
search argument. Normalize inputs:
$results = World::countries()->search(strtolower($searchTerm))->get();
API Route Overrides:
WorldController. If you override it, ensure you replicate all methods (e.g., countries(), states(), etc.) to avoid broken endpoints.Optional Fields:
currency_code or timezone are optional by default. If your app requires them, enable them in config/world.php and re-run the seeder:
'optional_fields' => [
'currencies' => true,
'timezones' => true,
],
php artisan db:seed --class=WorldSeeder). Check the countries, states, and cities tables directly if queries return empty.routes/web.php for the package’s API routes:
Route::prefix('api')->group(function () {
Route::get('/countries', [\Tocaan\World\Http\Controllers\WorldController::class, 'countries']);
// ... other routes
});
$cities = World::cities()->paginate(50);
Custom Models:
Country, State, City, etc.) to add custom logic or relationships:
// app/Models/CustomCountry.php
class CustomCountry extends \Tocaan\World\Models\Country {
public function regions() {
return $this->hasMany(CustomRegion::class);
}
}
Additional Data Sources:
country_phone_codes) and join them in queries:
$countries = World::countries()
->join('country_phone_codes', 'countries.code', '=', 'country_phone_codes.country_code')
->select('countries.*', 'country_phone_codes.code as phone_code')
->get();
Dynamic Seeding:
WorldSeeder to filter or transform data before seeding:
// database/seeders/CustomWorldSeeder.php
class CustomWorldSeeder extends \Tocaan\World\Database\Seeders\WorldSeeder {
protected function getCountries() {
return parent::getCountries()->where('region', 'Europe');
}
}
php artisan db:seed --class=CustomWorldSeeder
Testing:
public function test_country_retrieval() {
$countries = World::countries();
$this->assertCount(195, $countries); // Example assertion
}
WorldController or use the published routes.How can I help you explore Laravel packages today?