PHP code example of dev1191 / filament-nepali-address
1. Go to this page and download the library: Download dev1191/filament-nepali-address 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/ */
dev1191 / filament-nepali-address example snippets
use Illuminate\Database\Schema\Blueprint;
Schema::table('users', function (Blueprint $table) {
// Adds province_id, district_id, municipality_id, ward_no, and postal_code
$table->nepaliAddress();
// Or with custom prefix for multiple addresses (e.g. billing_ & shipping_):
$table->nepaliAddress('billing_');
$table->nepaliAddress('shipping_', withWard: false, withPostalCode: false);
});
Schema::table('users', function (Blueprint $table) {
$table->dropNepaliAddress();
$table->dropNepaliAddress('billing_');
});
namespace App\Models;
use Dev1191\FilamentNepaliAddress\Traits\HasNepaliAddress;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use HasNepaliAddress;
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'name',
'email',
'password',
// Nepali address columns:
'province_id',
'district_id',
'municipality_id',
'ward_no',
'postal_code',
];
}
use Dev1191\FilamentNepaliAddress\Forms\Components\NepaliAddressSelects;
public static function form(Form $form): Form
{
return $form
->schema([
// Default 3-column cascading select (Province, District, Municipality)
NepaliAddressSelects::make(),
]);
}
NepaliAddressSelects::make()
->onlyProvinces([3]) // Only Bagmati Pradesh
->onlyDistricts([5, 6, 7]) // Only Kathmandu, Lalitpur, and Bhaktapur
// or exclude specific areas:
->exceptProvinces([1, 2]);
use Dev1191\FilamentNepaliAddress\Infolists\Components\NepaliAddressEntry;
public static function infolist(Infolist $infolist): Infolist
{
return $infolist
->schema([
// Combined address entry
NepaliAddressEntry::make('address')
->withWard(),
// Single administrative levels
NepaliAddressEntry::province('province_id'),
NepaliAddressEntry::district('district_id'),
NepaliAddressEntry::localBody('municipality_id'),
]);
}
namespace App\Models;
use Dev1191\FilamentNepaliAddress\Traits\HasNepaliAddress;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use HasNepaliAddress;
/**
* Get the formatted address string.
*/
public function getAddressAttribute(): string
{
return $this->getNepaliAddress();
}
}
// Find all users in Bagmati Province (province_id = 3)
User::whereProvince(3)->get();
User::whereProvince(3, prefix: 'billing_')->get();
// Find all users in Kathmandu District (district_id = 5)
User::whereDistrict(5)->get();
User::whereDistrict(5, prefix: 'shipping_')->get();
// Find all users in a specific local body or ward
User::whereLocalBody(270)->get();
User::whereWard(4)->get();
User::whereWard(4, prefix: 'billing_')->get();
// Smart address search (English or Devanagari script)
User::whereNepaliAddress('Kathmandu')->get();
User::whereNepaliAddress('काठमाडौं')->get();
User::whereNepaliAddress('Pokhara', prefix: 'shipping_')->get();
use Dev1191\FilamentNepaliAddress\Support\AddressData;
// Get all provinces
$provinces = AddressData::getProvinces();
// Get districts in Bagmati (province_id = 3)
$districts = AddressData::getDistricts(3);
// Get local bodies in Kathmandu district (district_id = 5)
$localBodies = AddressData::getLocalBodies(5);
// Resolve names by ID
$name = AddressData::getProvinceName(3); // "Bagmati Pradesh"
$name = AddressData::getDistrictName(5); // "Kathmandu"
$name = AddressData::getLocalBodyName(270); // "Kathmandu Metropolitan City"
// Format full address
$address = AddressData::formatAddress(
provinceId: 3,
districtId: 5,
municipalityId: 270,
wardNo: 4,
separator: ', '
);
// "Kathmandu Metropolitan City-4, Kathmandu, Bagmati Pradesh"
return [
/*
| Options: 'en' for English, 'ne' for Devanagari Nepali
*/
'lang' => 'ne',
];