PHP code example of coldcoder / laravel-worldcities

1. Go to this page and download the library: Download coldcoder/laravel-worldcities library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

coldcoder / laravel-worldcities example snippets


return [

    /*
     * define the table names for continent, country, state and city
     */
    'table' => [
        'continent' => 'worlds_continents',

        'country' => 'worlds_countries',

        'state' => 'worlds_states',

        'city' => 'worlds_cities',
    ],
];

use Coldcoder\WorldCity\Models\Country;

// get a country by code
$usa = Country::findFromCode('us');
$usa->states;  // return states
$usa->cities;  // return cities
$usa->has_state; // return true;

// as it just implements locales of en and zh, you can translate other locales by yourself
// or request a PR
// translating a continent/country/state/city
$usa->setTranslation('name', 'fr', 'country name in French');
$usa->save();

// you can use HasCity trait within your own model to setup relationship
use Coldcoder\WorldCity\Traits\HasCity;

class YourModel extends Model
{
    use HasCity;
}

// after that you can get your model's related city
$model->city;
bash
php artisan vendor:publish --provider="Coldcoder\WorldCity\WorldCityServiceProvider"
bash
php artisan migrate
bash
composer dump-autoload
php artisan db:seed --class=WorldsTablesSeeder
bash
php artisan vendor:publish --provider="Coldcoder\WorldCity\WorldCityServiceProvider" --tag="config"