Get all Countries, Cities, Currencies, Timezones in Laravel

Get all Countries, Cities, Currencies, Timezones in Laravel cover image

Najm 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.

Installation

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?

  • Re-publish the package assets by issuing the command php artisan vendor:publish --tag=world --force
  • If needed, customize the table names and enable or disabled the optional database fields in the world.php config file.

Usage

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.

Written by

Rizwan Aslam

Founder and CEO of CodeBrisk, with years of experience building web applications with Laravel and leading a team of expert developers.

More about Rizwan
Book a free call
Book a call with CodeBrisk