This package contains world country list along with states, cities, country code and country flags.
Via Composer
$ composer require thedevsaddam/world-countries
Install manually (add the line to composer.json file)
"thedevsaddam/world-countries": "1.1"
Open your terminal and hit the command
composer update
Add the following line to config/app.php file
Thedevsaddam\WorldCountries\WorldCountriesServiceProvider::class,
To publish the configuration and resources
php artisan vendor:publish
If you need only flags then avoid the command below. The command will required when you are going to use countries with states, cities, country code etc
php artisan world-countries:populate
The above command will populate the countries and other tables to database
To drop the populated tables
php artisan world-countries:drop
- World country list with Official name, short name, flag, latitude, longitude.
- World country list with states and cities
- World Timezones (coming soon)
To fetch all the countries with flag. This will return a collection object so that you can use the available method of Collection class
WorldCountries::getCountriesWithFlag();
$countries = WorldCountries::getCountriesWithFlag();
foreach($countries as $key => $country){
echo "<img src='".$country['flagLargeUrl']."' />";
echo $country['name'];
echo "<br/><hr/>";
}
// you can use all the method of Collection class
$country = WorldCountries::getCountriesWithFlag()->where('name', 'Bangladesh');
$countries = WorldCountries::getCountriesWithStateCity();
//Note: This query will take some time to fetch all the countries with its associate states and cities
$countryModel = WorldCountries::getCountryModel();
//Fetch all the countries in array
$countries = $countryModel->get()->toArray();
//Fetch all the countries with states
$countriesWithStates = $countryModel->with('states')->get();
//Note: you can use Laravel eloquent available methods (See the below example)
$countryModel = WorldCountries::getCountryModel();
$country = $countryModel->where('name', 'Bangladesh')->first();
Note: You can get city and state model as well. To get them use the methods below
$stateModel = WorldCountries::getStateModel();
$cityModel = WorldCountries::getCityModel();
//now you can perform any query available to Eloquent model
//first get all the countries and populate the country dropdown
$countryModel = WorldCountries::getCountryModel();
$countries = $countryModel->lists('name', 'id');
//you can make an endpoint to fetch all the states against some individual country
$stateModel = WorldCountries::getStateModel();
$states = $stateModel->where('country_id', $someRequest->countryId)->get()->toArray();
return response()-json($states);
// now make another endpoint against the state and fetch all of it's cities.
$cityModel = WorldCountries::getCityModel();
$cities = $cityModel->where('state_id', $someRequest->stateId)->get()->toArray();
return response()->json($cities);
If you change the flag directory name or path then update the config/flag.php file_path according to the path name
Flag and country list json from: cristiroma
Countries, state and cities mysql dump from: cristiroma
Thank you :)