~/docs
Resonate
Resonate
2.x 2.2.2, current
Laravel Countries
Toggle sidebar
Laravel Countries
webpatser/laravel-countries 2.2.2

Laravel Countries 2.x

Installation

Install Laravel Countries and set up the configuration, the optional database table and the country data in your Laravel application.

Laravel Countries 2.2.2 ships its country data as a JSON file inside the package. You can use it right after installing, or import it into a database table.

Requirements

Requirement Version
PHP ^8.2
Laravel (illuminate/support, illuminate/database) ^11.0, ^12.0 or ^13.0
Database Optional. Only needed for the database data source and the Country model

Installation Steps

Step 1: Install via Composer

composer require webpatser/laravel-countries

The service provider and the Countries facade alias are registered through package discovery.

Step 2: Run the Installer (optional)

The package works with the bundled JSON file without any setup. To store the countries in your database, publish the config first, then run the interactive installer:

php artisan vendor:publish --tag=countries-config
php artisan countries:install

The installer:

  • Asks for the table name (default countries; skip the question with --table-name=)
  • Generates the migration, runs it if you confirm, then generates a CountriesSeeder and imports all 249 countries
  • Rewrites table_name, cache_ttl (to 0) and data_source (to database) in the already published config/countries.php; the installer fails if the config has not been published
  • Prints the installed flags

Use --force to overwrite an existing config, migration or seeder file:

php artisan countries:install --force

Manual Installation

Publish Configuration

php artisan vendor:publish --tag=countries-config

Generate and Run Migrations

php artisan countries:migration
php artisan migrate

countries:migration creates the migration and the seeder file and asks for confirmation first. It accepts --force to overwrite existing files.

Seed the Database

php artisan db:seed --class=CountriesSeeder

Then set data_source to database in config/countries.php.

Configuration

The published config/countries.php has these keys and defaults:

return [
'table_name' => 'countries',
 
'cache_ttl' => 3600, // seconds; 0 disables caching
 
'data_source' => 'json', // 'json' or 'database'
 
'default_sort' => 'name',
 
'localized' => false,
 
'search' => [
'fields' => ['name', 'full_name', 'capital'],
'case_sensitive' => false,
],
];
Key Default Meaning
table_name countries Table used by the Country model, the migration and the database data source
cache_ttl 3600 Cache lifetime in seconds for the loaded country list; 0 loads directly each time
data_source json json reads the bundled file, database reads the table
localized false When true, the Country model looks up countries.{ISO code} in your translation files and falls back to the stored name

Note: The installer writes data_source as database and cache_ttl as 0 into your published config. The package defaults are json and 3600.

Note: In 2.2.2 the database data source returns the fields name, capital, iso_3166_2, iso_3166_3, currency_code, currency_name, currency_symbol, calling_code, region, languages and flag. The full_name field is present in the JSON source and on the Country model, but not in arrays returned by Countries when data_source is database.

Upgrading an existing database install to 2.2

Version 2.2.0 added a nullable full_name column (the official name, for example "United States of America"). If you installed the table earlier, add the column yourself or re-run countries:install.

Verify Installation

use Webpatser\Countries\Countries;
 
$countries = new Countries();
$usa = $countries->getOne('US');
 
echo $usa['name']; // "United States"
echo $usa['full_name']; // "United States of America" (json data source)
echo $usa['flag']; // "🇺🇸"

Try it

Installation Working!

Laravel Countries is properly installed and functioning.

Test Country Code
Enter a 2-letter country code to test

Quick Tests:

🇺🇸

United States

US

Capital: Washington, D.C.
Region: americas
Currency: $ USD

Test Code Example:

use Webpatser\Countries\Countries;

$countries = new Countries();
$country = $countries->getOne('US');

echo $country['name'];     // "United States"
echo $country['flag'];     // "🇺🇸"
echo $country['capital'];  // "Washington, D.C."

Troubleshooting

Migration or seeder file already exists

Run the installer with --force:

php artisan countries:install --force

Database connection error

Check the database settings in .env:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database
DB_USERNAME=your_username
DB_PASSWORD=your_password

Next Steps