PHPackages                             kevinpurwito/laravel-country - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. kevinpurwito/laravel-country

ActiveLibrary[Database &amp; ORM](/categories/database)

kevinpurwito/laravel-country
============================

Country list migration, seeders and model for Laravel

v3.0.0(3y ago)02.7kMITPHPPHP ^8.0|^8.1

Since Aug 7Pushed 3y ago1 watchersCompare

[ Source](https://github.com/kevinpurwito/laravel-country)[ Packagist](https://packagist.org/packages/kevinpurwito/laravel-country)[ Docs](https://github.com/kevinpurwito/laravel-country)[ Fund](https://www.paypal.me/kevinpurwito)[ RSS](/packages/kevinpurwito-laravel-country/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (7)Dependencies (5)Versions (9)Used By (0)

About Laravel Country
=====================

[](#about-laravel-country)

[![Tests](https://github.com/kevinpurwito/laravel-country/actions/workflows/run-tests.yml/badge.svg?branch=main)](https://github.com/kevinpurwito/laravel-country/actions/workflows/run-tests.yml)[![Code Style](https://github.com/kevinpurwito/laravel-country/actions/workflows/php-cs-fixer.yml/badge.svg?branch=main)](https://github.com/kevinpurwito/laravel-country/actions/workflows/php-cs-fixer.yml)[![Psalm](https://github.com/kevinpurwito/laravel-country/actions/workflows/psalm.yml/badge.svg?branch=main)](https://github.com/kevinpurwito/laravel-country/actions/workflows/psalm.yml)[![Latest Version on Packagist](https://camo.githubusercontent.com/f93e533332be0d625a5f8b1f3a8d028b6c87022281958c57c7126f965e8e2bb3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6b6576696e7075727769746f2f6c61726176656c2d636f756e7472792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/kevinpurwito/laravel-country)[![Total Downloads](https://camo.githubusercontent.com/a89f549dbb4b6e7a4dfc693137ac34c784250b6d096503d0a6fab79e3b67e74a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6b6576696e7075727769746f2f6c61726176656c2d636f756e7472792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/kevinpurwito/laravel-country)

Laravel Country is a package containing country list migration, seeders and model for Laravel.

The list of countries is populated from

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require kevinpurwito/laravel-country

```

Configuration
-------------

[](#configuration)

The `vendor:publish` command will publish a file named `kp_country.php` within your laravel project config folder `config/kp_country.php`. Edit this file with your desired table name for the table, defaults to `countries`.

Published Config File Contents

```
[
    'table_names' => [
        'country' => env('KP_COUNTRY_TABLE_NAME', 'countries'),

        'province' => env('KP_COUNTRY_TABLE_NAME_PROVINCE', 'provinces'),

        'city' => env('KP_COUNTRY_TABLE_NAME_CITY', 'cities'),

        'district' => env('KP_COUNTRY_TABLE_NAME_DISTRICT', 'districts'),

        'ward' => env('KP_COUNTRY_TABLE_NAME_WARD', 'wards'),
    ],

    'popular_column' => env('KP_COUNTRY_POPULAR_COLUMN', true),

    'ordinal_column' => env('KP_COUNTRY_ORDINAL_COLUMN', true),
];

```

Alternatively you can ignore the above publish command and add this following variables to your `.env` file.

```
KP_COUNTRY_TABLE_NAME=countries
KP_COUNTRY_TABLE_NAME_PROVINCE=provinces
KP_COUNTRY_TABLE_NAME_CITY=cities
KP_COUNTRY_TABLE_NAME_DISTRICT=districts
KP_COUNTRY_TABLE_NAME_WARD=wards
KP_COUNTRY_POPULAR_COLUMN=true
KP_COUNTRY_ORDINAL_COLUMN=true

```

Auto Discovery
--------------

[](#auto-discovery)

If you're using Laravel 5.5+ you don't need to manually add the service provider or facade. This will be Auto-Discovered. For all versions of Laravel below 5.5, you must manually add the ServiceProvider &amp; Facade to the appropriate arrays within your Laravel project `config/app.php`

### Provider

[](#provider)

```
[
    Kevinpurwito\LaravelCountry\CountryServiceProvider::class,
];

```

Running the migration
---------------------

[](#running-the-migration)

The only thing that you need to publish is the migration, you shouldn't need to publish the others, such as seeders and config; unless you want to customize them

Countries only migration

```
php artisan vendor:publish --provider=Kevinpurwito\\LaravelCountry\\CountryServiceProvider --tag=lc-countries

```

All migrations (countries, provinces, cities, districts, wards)

```
php artisan vendor:publish --provider=Kevinpurwito\\LaravelCountry\\CountryServiceProvider --tag=lc-migrations

```

Running the seeders
-------------------

[](#running-the-seeders)

Countries seeder

```
php artisan db:seed --class=Kevinpurwito\\LaravelCountry\\Database\\Seeders\\CountriesSeeder

```

> countries `code` are from [ISO 3166](https://en.wikipedia.org/wiki/ISO_3166)

Indonesia's provinces, cities and districts seeder

```
php artisan db:seed --class=Kevinpurwito\\LaravelCountry\\Database\\Seeders\\IdProvincesSeeder
php artisan db:seed --class=Kevinpurwito\\LaravelCountry\\Database\\Seeders\\IdCitiesSeeder
php artisan db:seed --class=Kevinpurwito\\LaravelCountry\\Database\\Seeders\\IdDistrictsSeeder

```

> Indonesian provinces `iso2` are from [ISO 3166-2:ID](https://en.wikipedia.org/wiki/ISO_3166-2:ID)

> Indonesian provinces, cities and districts `code` are from [Kemendagri](https://id.wikipedia.org/wiki/Daftar_kecamatan_dan_kelurahan_di_Indonesia)

Usage
-----

[](#usage)

Country class

```
use Kevinpurwito\LaravelCountry\Models\Country;

// Get a country by name, iso2 or iso3
$country = Country::findByName('Indonesia');
$country = Country::findByIso2('ID');
$country = Country::findByIso3('IDN');

// mark a country as popular
$country->setPopular(true);

// set `ordinal` of the country
$country->setOrdinal(1);

// get list of countries by default ordering (`popular` first, after that by `ordinal` and finally by `name`)
$countries = Country::default()->get();

// get list of provinces in a country
$provinces = $country->provinces()->default()->get();

// get list of cities in a province
$cities = $provinces[0]->cities()->default()->get();

```

### Adding relationship to your own model

[](#adding-relationship-to-your-own-model)

```
use Kevinpurwito\LaravelCountry\Relationships\BelongsToCountry;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use BelongsToCountry;

    // now you can use `country` relationship to your user model, given that your table has 'country_id' column
    // e.g. $user->country->iso2; returns 'ID' if your user belongs to 'Indonesia' country
}

```

### Testing

[](#testing)

```
composer test

```

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

### Security

[](#security)

If you discover any security related issues, please email instead of using the issue tracker.

Credits
-------

[](#credits)

- [Kevin Purwito](https://github.com/kevinpurwito)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

Laravel Package Boilerplate
---------------------------

[](#laravel-package-boilerplate)

This package was generated using the [PHP Package Boilerplate](https://laravelpackageboilerplate.com)by [Beyond Code](http://beyondco.de/)with some modifications inspired from [PHP Package Skeleton](https://github.com/spatie/package-skeleton-php)by [spatie](https://spatie.be/).

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~41 days

Recently: every ~72 days

Total

8

Last Release

1450d ago

Major Versions

1.1.1 → v2.0.02021-09-01

v2.0.1 → v3.0.02022-05-24

PHP version history (2 changes)1.0.0PHP ^7.4|^8.0

v3.0.0PHP ^8.0|^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/47e3d5e05f33c7b66c0c646fbce5dac5a319a8fd7ebb13977d2a1ae3df91e9a4?d=identicon)[kevinpurwito](/maintainers/kevinpurwito)

---

Top Contributors

[![kevinpurwito](https://avatars.githubusercontent.com/u/11625012?v=4)](https://github.com/kevinpurwito "kevinpurwito (30 commits)")

---

Tags

laravelcountrieskevinpurwito

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/kevinpurwito-laravel-country/health.svg)

```
[![Health](https://phpackages.com/badges/kevinpurwito-laravel-country/health.svg)](https://phpackages.com/packages/kevinpurwito-laravel-country)
```

###  Alternatives

[rtconner/laravel-likeable

Trait for Laravel Eloquent models to allow easy implementation of a 'like' or 'favorite' or 'remember' feature.

394388.0k5](/packages/rtconner-laravel-likeable)[lwwcas/laravel-countries

A comprehensive package for managing country data in Laravel applications, including multilingual support, geographic coordinates, and detailed metadata for seamless integration with Laravel.

12464.0k](/packages/lwwcas-laravel-countries)[io238/laravel-iso-countries

Ready-to-use Laravel models and relations for country (ISO 3166), language (ISO 639-1), and currency (ISO 4217) information with multi-language support.

5462.3k](/packages/io238-laravel-iso-countries)[altwaireb/laravel-world

Laravel World, Countries States Cities DB Migration &amp; Seeder

989.4k](/packages/altwaireb-laravel-world)[phaza/single-table-inheritance

Single Table Inheritance Trait

1515.8k](/packages/phaza-single-table-inheritance)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
