05
AprNajm Njeim introduced an amazing Laravel package called World that provides a list of the countries, states, cities, timezones, currencies, and languages. It can be consumed with the World Facade or the defined API routes.
You can install this package via composer by running this command in your terminal.
composer require nnjeim/world
Next, you have to run these commands.
php artisan vendor:publish --tag=world
php artisan migrate
php artisan db:seed --class=WorldSeeder (requires ~15min)
Upgrading to v1.1.6?
php artisan vendor:publish --tag=world --force
world.php
config file.List all the countries
Use the World facade
use Nnjeim\World\World;
$action = World::countries();
if ($action->success) {
$countries = $action->data;
}
response
{
"success": true,
"message": "countries",
"data": [
{
"id": 1,
"name": "Afghanistan"
},
{
"id": 2,
"name": "Åland Islands"
},
.
.
.
],
}
Use the API countries endpoint
https://myDomain.local/api/countries
Fetch a country with its states and cities
Use the World facade
use Nnjeim\World\World;
$action = World::countries([
'fields' => 'states,cities',
'filters' => [
'iso2' => 'FR',
]
]);
if ($action->success) {
$countries = $action->data;
}
response
{
"success": true,
"message": "countries",
"data": [
"id": 77,
"name": "France",
"states": [
{
"id": 1271,
"name": "Alo"
},
{
"id": 1272,
"name": "Alsace"
},
.
.
.
],
"cities": [
{
"id": 25148,
"name": "Abondance"
},
{
"id": 25149,
"name": "Abrest"
},
.
.
.
]
],
}
Use the API countries endpoint
https://myDomain.local/api/countries?fields=states,cities&filters[iso2]=FR
List all the cities by country id
use Nnjeim\World\WorldHelper;
protected $world;
public function __construct(WorldHelper $world) {
$this->world = $world;
}
$action = $this->world->cities([
'filters' => [
'country_id' => 182,
],
]);
if ($action->success) {
$cities = $action->data;
}
Use the API cities endpoint
https://myDomain.local/api/cities?filters[country_id]=182
For more features & details about this package, you can visit its complete documentation and source code on Github.
Published at : 05-04-2022
I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.
Launch project