Home

Awesome

Laravel World

This package will allow you to add all Countries, States, Cities Data DB Migration & Seeder for Laravel

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Numbers

ModelNumber of items
Country250
State4961
City148059

Attributes

Common attributes:

Installation

You can install the package via composer:

composer require altwaireb/laravel-world

Usage

Now run the following command to install .

php artisan world:install

Add seeder File in database\seeders\DatabaseSeeder.php add this line to use php artisan db:seed command.

public function run(): void
    {
    
        $this->call(WorldTableSeeder::class);
        ...
    }

Or you can Seeding Data of Countries States Cities, by run this command.

php artisan world:seeder

And you can refresh to re-seeding Data of Countries States Cities, by run this command.

php artisan world:seeder --refresh

You can specify the activation of countries through the country code ISO2 or ISO3, before processing the seed data in the config file. config/world.php

return [
    'insert_activations_only' => false,
    'countries' => [
        'activation' => [
            'default' => true,
            'only' => [
                'iso2' => ['SA','GB','DE'],
                'iso3' => ['USA','BRA','EGY'],
            ],
            'except' => [
                'iso2' => ['GA'],
                'iso3' => ['HTI'],
            ],
        ],
        'chunk_length' => 50,
    ],

    'states' => [
        'activation' => [
            'default' => true,
        ],
        'chunk_length' => 200,
    ],

    'cities' => [
        'activation' => [
            'default' => true,
        ],
        'chunk_length' => 200,
    ],
];

If you need to insert the countries is activation , this insert only two Countries ( Albania , Argentina ) with States and Cities.

return [
    'insert_activations_only' => true,
    'countries' => [
        'activation' => [
            'default' => true,
            'only' => [
                'iso2' => ['AL','AR'],
                'iso3' => [],
            ],
            'except' => [
                'iso2' => [],
                'iso3' => [],
            ],
        ],
        'chunk_length' => 50,
    ],

    ...
];

This means that only these two countries and the states and cities affiliated with them will be activated.

Usage

you can get country by iso2 and iso3 or both.

if you want to get country by iso2 you can yes static function getByIso2

use App\Models\Country;

$sa = Country::getByIso2('SA');
$sa->name; // Saudi Arabia
$sa->iso2; // SA
$sa->iso3; // SAU
$sa->currency_symbol; // ﷼
$sa->native; // المملكة العربية السعودية

if you want to get country by iso3 you can use.

use App\Models\Country;

$bra = Country::getByIso3('BRA');
$bra->name; // Brazil
$bra->iso2; // BR
$bra->iso3; // BRA
$bra->currency_symbol; // R$
$bra->native; // Brasil

also if you want to get country by code iso2 ro iso3 you can use.

use App\Models\Country;

$bra = Country::getByCode('PT');
$bra->name; // Portugal
$bra->iso2; // PT
$bra->iso3; // PRT
$bra->currency_symbol; // €
$bra->native; // Portugal

Scope

you can use Scope to filter data is Active by use.

use App\Models\Country;

$countries = Country::active()->pluck('name','id');

Credits

License

The MIT License (MIT). Please see License File for more information.