PHP code example of aliziodev / laravel-indonesia-regions

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

    

aliziodev / laravel-indonesia-regions example snippets


// Parameter harus sesuai urutan
$cities = Indonesia::getRegions('11', ['code', 'name'], 15);

// Urutan tidak penting, lebih jelas dan mudah dibaca
$cities = Indonesia::getRegions(
    parentCode: '11',
    columns: ['code', 'name'],
    perPage: 15
);

use Aliziodev\IndonesiaRegions\Facades\Indonesia;

// Mengambil semua provinsi
$provinces = Indonesia::getRegions();

// Response:
[
    {
        "code": "11",
        "name": "ACEH",
    },
    {
        "code": "12",
        "name": "SUMATERA UTARA",
    }
]

// Mengambil kota/kabupaten di Aceh dengan pagination
$cities = Indonesia::getRegions('11', ['code', 'name', 'latitude', 'longitude'], 15);

// Response dengan pagination:
{
    "current_page": 1,
    "data": [
        {
            "code": "11.01",
            "name": "KAB. ACEH SELATAN",
            "latitude": 3.31467,
            "longitude": 97.3517
        },
        // ... more cities
    ],
    "first_page_url": "http://example.com/api?page=1",
    "from": 1,
    "last_page": 23,
    "last_page_url": "http://example.com/api?page=23",
    "next_page_url": "http://example.com/api?page=2",
    "path": "http://example.com/api",
    "per_page": 15,
    "prev_page_url": null,
    "to": 15,
    "total": 343
}

// postal_code hanya muncul untuk desa/kelurahan

use Aliziodev\IndonesiaRegions\Facades\Indonesia;

// Pencarian umum
$results = Indonesia::search('Bakongan');

// Response:
[
    {
        "code": "11.01.01",
        "name": "BAKONGAN"
    },
    {
        "code": "11.01.01.2001",
        "name": "KEUDE BAKONGAN",
        "postal_code": "23773"  // postal_code hanya muncul untuk desa/kelurahan
    }
]

// Pencarian dengan tipe spesifik
$villages = Indonesia::search('Bakongan', 'village');

// Pencarian dengan kolom tambahan
$results = Indonesia::search('Bakongan', null, null, ['code', 'name', 'latitude', 'longitude']);

// Pencarian lengkap dengan named parameters (PHP 8.0+)
$results = Indonesia::search(
    term: 'Bakongan',
    type: 'village',
    perPage: 15,
    columns: ['code', 'name', 'latitude', 'longitude']
);

// Response dengan pagination sama dengan format getRegions()

use Aliziodev\IndonesiaRegions\Facades\Indonesia;

// Pencarian umum dengan alamat lengkap
$results = Indonesia::searchWithAddress('Bakongan');

// Response:
[
    {
        "code": "11.01.01",
        "name": "BAKONGAN",
        "full_address": "BAKONGAN, KAB. ACEH SELATAN, ACEH"
    },
    {
        "code": "11.01.01.2001",
        "name": "KEUDE BAKONGAN",
        "postal_code": "23773",
        "full_address": "KEUDE BAKONGAN, BAKONGAN, KAB. ACEH SELATAN, ACEH, 23773"
    }
]

// Pencarian dengan tipe spesifik
$villages = Indonesia::searchWithAddress('Bakongan', 'village');

// Pencarian dengan kolom tambahan
$results = Indonesia::searchWithAddress('Bakongan', null, null, ['code', 'name', 'latitude', 'longitude']);

// Pencarian lengkap dengan named parameters (PHP 8.0+)
$results = Indonesia::searchWithAddress(
    term: 'Bakongan',
    type: 'village',
    perPage: 15,
    columns: ['code', 'name', 'latitude', 'longitude']
);

// Response dengan pagination sama dengan format getRegions()

// Menggunakan nama negara kustom
$results = Indonesia::searchWithAddress('Bakongan', null, null, null, 'Republic of Indonesia');


use Aliziodev\IndonesiaRegions\Facades\Indonesia;

// Pencarian full text dengan default limit (15)
$results = Indonesia::searchWithFullText('Bakongan');

// Pencarian dengan limit kustom
$results = Indonesia::searchWithFullText('Bakongan', 25);

// Pencarian dengan nama negara kustom
$results = Indonesia::searchWithFullText(
    term: 'Bakongan',
    limit: 25,
    countryName: 'Republic of Indonesia'
);

// Response:
[
    {
        "code": "11.01.01.2001",
        "province": "ACEH",
        "city": "KAB. ACEH SELATAN",
        "district": "BAKONGAN",
        "village": "KEUDE BAKONGAN",
        "postal_code": "23773",
        "full_address": "KEUDE BAKONGAN, BAKONGAN, KAB. ACEH SELATAN, ACEH, REPUBLIC OF INDONESIA, 23773"
    }
    // ... more results
]

// Regular parameters
$region = Indonesia::findByCode('11.01.01.2001');
$region = Indonesia::findByCode('11.01.01.2001', ['code', 'name']);

// Named parameters (PHP 8.0+)
$region = Indonesia::findByCode(
    code: '11.01.01.2001',
    columns: ['code', 'name', 'latitude', 'longitude']
);

// Response:
{
    "code": "11.01.01.2001",
    "name": "KEUDE BAKONGAN",
    "postal_code": "23773",
    "latitude": 3.1618538408941346,
    "longitude": 97.43651771865193,
}

$provinces = Indonesia::getForSelect();

// Response:
{
    "11": "ACEH",
    "12": "SUMATERA UTARA",
    "13": "SUMATERA BARAT"
    // ... more provinces
}

// Get cities for select
$cities = Indonesia::getForSelect('11');

// Response:
{
    "11.01": "KAB. ACEH SELATAN",
    "11.02": "KAB. ACEH TENGGARA",
    // ... more cities
}


$info = Indonesia::getRegionInfo('11.01.01.2001');

// Response:
{
    "province": {
        "code": "11",
        "name": "ACEH"
    },
    "city": {
        "code": "11.01",
        "name": "KAB. ACEH SELATAN"
    },
    "district": {
        "code": "11.01.01",
        "name": "BAKONGAN"
    },
    "village": {
        "code": "11.01.01.2001",
        "name": "KEUDE BAKONGAN",
        "postal_code": "23773"
    },
    "full_address": "KEUDE BAKONGAN, BAKONGAN, KAB. ACEH SELATAN, ACEH, 23773"
}

// Menggunakan nama negara kustom
$info = Indonesia::getRegionInfo('11.01.01.2001', null, 'Republic of Indonesia');
// Response akan menggunakan nama negara yang dikustomisasi
"KEUDE BAKONGAN, BAKONGAN, KAB. ACEH SELATAN, ACEH, Republic of Indonesia, 23773"

$address = Indonesia::getFullAddress('11.01.01.2001');

// Response:
"KEUDE BAKONGAN, BAKONGAN, KAB. ACEH SELATAN, ACEH, 23773"


// Menggunakan nama negara kustom
$address = Indonesia::getFullAddress('11.01.01.2001', 'Republic of Indonesia');
// Response akan menggunakan nama negara yang dikustomisasi
"KEUDE BAKONGAN, BAKONGAN, KAB. ACEH SELATAN, ACEH, Republic of Indonesia, 23773"

$region = Indonesia::findByPostalCode('23773');
// Response:
{
    "code": "11.01.01.2001",
    "name": "KEUDE BAKONGAN",
    "postal_code": "23773"
}

$isValid = Indonesia::validateCode('11.01.01.2001'); // true
$isValid = Indonesia::validateCode('11.99'); // false
$isValid = Indonesia::validateCode('11.12345'); // false

$type = Indonesia::getRegionType('11.01.01.2001'); // 'village'
$type = Indonesia::getRegionType('11.01.01'); // 'district'
$type = Indonesia::getRegionType('11.01'); // 'city'
$type = Indonesia::getRegionType('11'); // 'province'
bash
php artisan indonesia-regions:install
bash
php artisan indonesia-regions:clear-cache