Installation Guide

Get started with Laravel Countries in your Laravel application.

Requirements

Requirement
Version
Status
PHP 8.2+
βœ… Required
Laravel ^11.0 | ^12.0
βœ… Required
Database MySQL, PostgreSQL, SQLite, SQL Server
πŸ“‹ Optional

Installation Steps

Step 1: Install via Composer

Install the package using Composer:

composer require webpatser/laravel-countries

Step 2: Run the Installer

Use the interactive installer to set up the database and configuration:

php artisan countries:install

The installer will:

  • Ask for your preferred database table name (defaults to countries)
  • Generate and run database migrations
  • Import all 249 countries with flags, currencies, and regional data
  • Publish the configuration file
  • Display all country flags to verify successful installation

Manual Installation

If you prefer to set up the package manually:

Publish Configuration

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

Generate and Run Migrations

php artisan countries:migration
php artisan migrate

Seed the Database

php artisan db:seed --class=CountriesSeeder

Configuration

The configuration file is published to config/countries.php:

return [
/*
|--------------------------------------------------------------------------
| Database Table Name
|--------------------------------------------------------------------------
*/
'table_name' => 'countries',
 
/*
|--------------------------------------------------------------------------
| Cache Settings
|--------------------------------------------------------------------------
*/
'cache_ttl' => 0, // Disable caching (recommended for database source)
 
/*
|--------------------------------------------------------------------------
| Data Source
|--------------------------------------------------------------------------
*/
'data_source' => 'database', // 'database' or 'json'
 
/*
|--------------------------------------------------------------------------
| Default Sort Field
|--------------------------------------------------------------------------
*/
'default_sort' => 'name',
 
/*
|--------------------------------------------------------------------------
| Localization
|--------------------------------------------------------------------------
*/
'localized' => false,
 
/*
|--------------------------------------------------------------------------
| Search Settings
|--------------------------------------------------------------------------
*/
'search' => [
'fields' => ['name', 'capital'],
'case_sensitive' => false,
],
];

Verify Installation

Test your installation with this simple code:

use Webpatser\Countries\Countries;
 
$countries = new Countries();
$usa = $countries->getOne('US');
 
echo $usa['name']; // "United States"
echo $usa['flag']; // "πŸ‡ΊπŸ‡Έ"
Test Your Installation
Use our interactive tester to verify your installation is working correctly:

Installation Working!

Laravel Countries is properly installed and functioning.

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

Quick Tests:

πŸ‡ΊπŸ‡Έ

United States of America

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 of America"
echo $country['flag'];     // "πŸ‡ΊπŸ‡Έ"
echo $country['capital'];  // "Washington, D.C."

Troubleshooting

Common Issues

Migration Already Exists
If you see "Migration already exists", use the --force flag:
php artisan countries:install --force
Database Connection Error
Ensure your database configuration is correct 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

Package Not Found

If Composer cannot find the package, ensure you have added the correct repository or the package has been published to Packagist.