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/ */
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
}