PHP code example of imujas9 / laravel-world

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

    

imujas9 / laravel-world example snippets


use Imujas9\World\Facades\Country;
use Imujas9\World\Facades\State;
use Imujas9\World\Facades\City;

Country::all();                          // all countries, names in app locale
Country::find(101);                      // by numeric ID
Country::findByCode('IN');               // by ISO2 code

State::whereCountry('IN')->get();        // states of India
State::find(12);                         // by numeric ID
State::findByCode('GJ');                 // by code

City::whereState('GJ')->get();           // cities of Gujarat
City::whereCountry('IN')->whereState('MH')->get();

// Explicit language
Country::lang('hi')->get();              // Hindi names
Country::lang('en', 'hi')->get();        // both → name_en, name_hi

// Locale-aware — no lang() needed when app locale is set
App::setLocale('hi');
Country::all();                          // names in Hindi automatically

// config/world.php
return [
    // 'file' (default, zero setup) or 'database'
    'driver' => env('WORLD_DRIVER', 'file'),

    // Fallback language. Priority: app locale → this → 'en'
    'default_lang' => env('WORLD_LANG', 'en'),

    // Table prefix for database driver
    'table_prefix' => 'world_',

    // Storage path for downloaded city translations
    'city_translations_path' => storage_path('app/world/translations/cities'),
];

App::setLocale($request->user()?->locale ?? 'en');

// Anywhere downstream — no lang() needed:
Country::all();                    // names in user's language
Country::findByCode('IN');         // name in locale
State::whereCountry('IN')->get();  // state names in locale

City::lang('hi')->whereState('GJ')->get();

Country::all();
Country::find(101);                              // by numeric ID
Country::findByCode('IN');                       // by ISO2 (case-insensitive)
Country::lang('hi')->get();                      // single language
Country::lang('en', 'hi')->get();                // multiple → name_en, name_hi
Country::lang('en')->whereRegion('Asia')->get();
Country::lang('en')->where('currency', 'INR')->get();
Country::lang('en')->where('id', '>', 50)->get();    // operators: >, <, >=, <=, !=
Country::lang('en')->whereLike('name', 'Ind%')->get();
Country::lang('en')->search('india')->get();         // partial name match
Country::lang('en')->whereIn('code', ['IN', 'US', 'GB'])->get();
Country::lang('en')->orderBy('name')->get();
Country::lang('en')->orderBy('name', 'desc')->get();
Country::lang('en')->limit(10)->offset(20)->get();
Country::lang('en')->whereRegion('Asia')->first();
Country::whereRegion('Europe')->count();
Country::lang('en')->paginate(15);               // reads ?page from request
Country::lang('en')->paginate(15, 2);            // explicit page 2

State::all();
State::find(12);                                 // by numeric ID
State::findByCode('GJ');                         // by code (case-insensitive)
State::findByCode('CA', 'US');                   // disambiguate — CA exists in US and Canada
State::whereCountry('IN')->get();
State::lang('hi')->whereCountry('IN')->get();
State::lang('en', 'hi')->whereCountry('IN')->get();
State::whereCountry('US')->count();
State::whereCountry('IN')->paginate(20);
State::whereCountry('IN')->search('gujar')->get();  // partial name match
State::whereIn('country_code', ['IN', 'US'])->get();

City::all();
City::find(1);                                   // by numeric ID
City::whereCountry('IN')->get();
City::whereState('GJ')->get();
City::whereCountry('IN')->whereState('MH')->get();
City::lang('hi')->whereState('GJ')->get();
City::lang('en', 'hi')->whereState('GJ')->get();
City::whereCountry('IN')->paginate(50);
City::whereCountry('IN')->search('mumbai')->get();   // partial name match
City::whereIn('state_code', ['GJ', 'MH'])->get();

Country::where('currency', 'INR')->get();
Country::where('region', 'Asia')->get();

Country::where('id', '>', 100)->get();
Country::where('id', '>=', 50)->count();
Country::where('id', '!=', 1)->get();

// whereLike uses SQL-style % and _ wildcards
Country::lang('en')->whereLike('name', 'United%')->get();   // starts with
Country::lang('en')->whereLike('name', '%land')->get();     // ends with
Country::lang('en')->whereLike('name', '%istan%')->get();   // contains

// search() is shorthand for whereLike(field, '%term%')
Country::lang('en')->search('india')->get();
State::whereCountry('IN')->search('gujar')->get();
City::whereCountry('IN')->search('mumbai')->get();

// search on a different field
Country::lang('en')->search('Delhi', 'capital')->get();

Country::whereIn('code', ['IN', 'US', 'GB'])->get();
State::whereIn('country_code', ['IN', 'US'])->get();
City::whereIn('state_code', ['GJ', 'MH', 'DL'])->get();

City::whereCountry('IN')
    ->whereIn('state_code', ['GJ', 'MH'])
    ->search('abad')
    ->orderBy('name')
    ->paginate(20);

// In a controller — page comes from ?page=N in the URL
$countries = Country::lang('en')->whereRegion('Asia')->paginate(15);
$states    = State::whereCountry('IN')->paginate(20);
$cities    = City::whereCountry('IN')->paginate(50);

// Explicit page (useful outside HTTP context)
$page2 = Country::lang('en')->paginate(15, 2);

// Migration
$table->unsignedBigInteger('country_id')->nullable();
$table->unsignedBigInteger('state_id')->nullable();
$table->unsignedBigInteger('city_id')->nullable();

use Imujas9\World\Traits\HasWorldRelations;

class User extends Authenticatable
{
    use HasWorldRelations;
}

$user->country;             // CountryData (both drivers)
$user->country->name;       // "India"
$user->country->flag;       // "🇮🇳"
$user->country->capital;    // "New Delhi"
$user->country->phone_code; // "91"
$user->state->name;         // "Gujarat"
$user->city->name;          // "Ahmedabad"

// Eager loading (database driver only)
User::with('country', 'state', 'city')->get();
// $user->country still returns CountryData, not a raw model

class User extends Authenticatable
{
    use HasCountry, HasState;

    protected string $countryForeignKey = 'country_code';
    protected string $countryOwnerKey   = 'code';

    protected string $stateForeignKey   = 'state_code';
    protected string $stateOwnerKey     = 'code';
}
bash
php artisan vendor:publish --tag=world-config
bash
php artisan vendor:publish --tag=world-migrations
php artisan migrate
php artisan world:seed