1. Go to this page and download the library: Download hifzi/wilayah-indonesia 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/ */
hifzi / wilayah-indonesia example snippets
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Village extends Model
{
// Sesuaikan prefix tabel dengan config Anda
protected $table = 'indonesia_villages';
protected $primaryKey = 'code';
public $incrementing = false;
protected $keyType = 'string';
public function district()
{
return $this->belongsTo(District::class, 'district_code', 'code');
}
}
use Hifzi\WilayahIndonesia\Database\Seeders\WilayahIndonesiaSeeder as IndonesiaSeeder;
public function run(): void
{
$this->call([
IndonesiaSeeder::class,
]);
}
use Hifzi\WilayahIndonesia\Facades\IndReg;
// Ambil Semua Provinsi
$provinces = IndReg::getProvinces();
// Return: [['code' => '11', 'name' => 'Aceh'], ...]
// Ambil Kota berdasarkan Kode Provinsi
$cities = IndReg::getCitiesByProvince('11');
// Return: [['code' => '1101', 'name' => 'Kabupaten Aceh Selatan'], ...]
// Ambil Kecamatan berdasarkan Kode Kota
$districts = IndReg::getDistrictsByCity('1101');
// Return: [['code' => '110101', 'name' => 'Bakongan'], ...]
// Ambil Desa berdasarkan Kode Kecamatan
$villages = IndReg::getVillagesByDistrict('110101');
// Return: [['code' => '1101012001', 'name' => 'Keude Bakongan', 'postal_code' => '23773'], ...]