PHP code example of kevinpurwito / laravel-country

1. Go to this page and download the library: Download kevinpurwito/laravel-country 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/ */

    

kevinpurwito / laravel-country example snippets


[
    '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),
];

[
    Kevinpurwito\LaravelCountry\CountryServiceProvider::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();

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
}

bash
php artisan vendor:publish --provider=Kevinpurwito\\LaravelCountry\\CountryServiceProvider --tag=lc-countries
bash
php artisan vendor:publish --provider=Kevinpurwito\\LaravelCountry\\CountryServiceProvider --tag=lc-migrations
bash
php artisan db:seed --class=Kevinpurwito\\LaravelCountry\\Database\\Seeders\\CountriesSeeder
bash
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