PHP code example of jszdavid / laravel-world

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

    

jszdavid / laravel-world example snippets


// config/laravel-world.php

return [
    'countries' => [
        'only' => null,   // Array of ISO2 codes to e,
        'prefix' => 'api',
    ],

    'migrations' => [
        'countries' => [
            'table_name' => 'laravel-world-countries',
        ],
        'states' => [
            'table_name' => 'laravel-world-states',
        ],
        'cities' => [
            'table_name' => 'laravel-world-cities',
        ],
    ],
    
    'cache_ttl' => 604800, // 1 week
];

use JSzD\World\Facades\World;

// Get all countries (default fields: id, name, iso2)
$countries = World::countries([])->data;

// Get countries with specific fields and search
$countries = World::countries([
    'fields' => 'id,name,iso2,phone_code',
    'search' => 'United',
])->data;

// Filter countries
$countries = World::countries([
    'filters' => [
        'iso2' => 'US'
    ]
])->data;

// Get states for a country
$states = World::states([
    'filters' => [
        'country_code' => 'US'
    ]
])->data;

// Get cities for a state
$cities = World::cities([
    'filters' => [
        'state_id' => 123
    ]
])->data;

// Get cities for a country
$cities = World::cities([
    'filters' => [
        'country_code' => 'US'
    ]
])->data;

$countries = World::withCaching()->countries([])->data;

public function country()
{
    return $this->belongsTo(\JSzD\World\Models\Country::class);
}
bash
php artisan vendor:publish --provider="JSzD\World\WorldServiceProvider"

php artisan migrate

php -d memory_limit=512M artisan db:seed --class=WorldSeeder