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-configphp 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
CountriesSeederand imports all 249 countries - Rewrites
table_name,cache_ttl(to0) anddata_source(todatabase) in the already publishedconfig/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:migrationphp 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_sourceasdatabaseandcache_ttlas0into your published config. The package defaults arejsonand3600.
Note: In 2.2.2 the
databasedata source returns the fieldsname,capital,iso_3166_2,iso_3166_3,currency_code,currency_name,currency_symbol,calling_code,region,languagesandflag. Thefull_namefield is present in the JSON source and on theCountrymodel, but not in arrays returned byCountrieswhendata_sourceisdatabase.
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.
Quick Tests:
United States
US
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=mysqlDB_HOST=127.0.0.1DB_PORT=3306DB_DATABASE=your_databaseDB_USERNAME=your_usernameDB_PASSWORD=your_password
Next Steps
- Quick Start: Learn the basics with practical examples.
- Eloquent Models: Use the Country model with relationships and scopes.