PHP code example of ajangsupardi / laravel-postcode-id
1. Go to this page and download the library: Download ajangsupardi/laravel-postcode-id 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/ */
ajangsupardi / laravel-postcode-id example snippets
use Ajangsupardi\PostcodeId\Database\Seeders\PostcodeSeeder;
public function run(): void
{
$this->call([
PostcodeSeeder::class,
]);
}
use Illuminate\Support\Facades\Artisan;
$storagePath = config('postcode.storage_path');
if (! file_exists($storagePath.'/kodepos.csv')) {
Artisan::call('postcode:download');
}
use Ajangsupardi\PostcodeId\Models\Village;
// Village by postal code
$village = Village::where('postal_code', '60111')->first();
// With full address hierarchy
$village = Village::with('district.regency.province')
->where('postal_code', '60111')
->first();
// Result: Gubeng → Kota Surabaya → Jawa Timur
// Search by village name
$villages = Village::where('name', 'LIKE', '%Gubeng%')
->with('district.regency')
->get();
namespace App\Models;
use Ajangsupardi\PostcodeId\Models\Province as BaseProvince;
class Province extends BaseProvince
{
protected $fillable = ['name', 'code', 'your_custom_field'];
// Add custom relationships or methods
}
use Ajangsupardi\PostcodeId\Models\Province;
use Ajangsupardi\PostcodeId\Models\Village;
// Get all provinces
$provinces = Province::all();
// Get regencies in East Java
$regencies = Province::where('name', 'Jawa Timur')
->first()
->regencies;
// Find a village by postal code
$village = Village::where('postal_code', '60111')->first();
// Get full address hierarchy from village to province
$village = Village::with('district.regency.province')
->where('postal_code', '60111')
->first();