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('billing_'),
NepaliAddressSelects::make('shipping_'),

NepaliAddressSelects::make()
    ->withWard() // Adds numeric 'ward_no' input
    ->withPostalCode() // Adds validated Nepal postal code input

NepaliAddressSelects::make()
    ->withWard(
        fieldName: 'ward_no',
        options: array_combine(range(1, 15), range(1, 15))
    )

NepaliAddressSelects::make()
    ->trictRequired()
    ->localBodyRequired()
    ->columns(3) // Customize grid layout
    ->relationshipValidation(true); // Enforces parent-child hierarchy validation rules

NepaliAddressSelects::make()
    ->onlyProvinces([3]) // Only Bagmati Pradesh
    ->onlyDistricts([5, 6, 7]) // Only Kathmandu, Lalitpur, and Bhaktapur
    // or exclude specific areas:
    ->exceptProvinces([1, 2]);

NepaliAddressSelects::makeProvinceSelect('province_id'),
NepaliAddressSelects::makeDistrictSelect('district_id', provinceFieldName: 'province_id'),
NepaliAddressSelects::makeLocalBodySelect('municipality_id', districtFieldName: 'district_id'),
NepaliAddressSelects::makePostalCodeInput('postal_code'),

use Dev1191\FilamentNepaliAddress\Columns\NepaliAddressColumn;

public static function table(Table $table): Table
{
    return $table
        ->columns([
            // Combined format: "Kathmandu-3, Kathmandu, Bagmati Pradesh"
            NepaliAddressColumn::make('address')
                ->searchable() // Smart search across local bodies, districts & provinces!
                ->sortable(),  // Multi-column sorting across province, district & municipality!

            // Combined with ward:
            NepaliAddressColumn::make('address')
                ->withWard(),

            // Multi-address prefix
            NepaliAddressColumn::make('shipping_address')
                ->prefix('shipping_')
                ->sortable(),
        ]);
}

NepaliAddressColumn::province('province_id')->sortable(),
NepaliAddressColumn::district('district_id')->sortable(),
NepaliAddressColumn::localBody('municipality_id')->sortable(),
NepaliAddressColumn::ward('ward_no')->sortable(),

use Dev1191\FilamentNepaliAddress\Filters\NepaliAddressFilter;

public static function table(Table $table): Table
{
    return $table
        ->filters([
            // Cascading filter: Province -> District -> Municipality
            NepaliAddressFilter::make(),

            // With Ward filter
            NepaliAddressFilter::make()->withWard(),

            // Restrict filter to specific territory
            NepaliAddressFilter::make()
                ->onlyProvinces([3])
                ->onlyDistricts([5, 6, 7]),

            // Multi-address prefix
            NepaliAddressFilter::make('shipping_address')
                ->prefix('shipping_'),
        ]);
}

NepaliAddressFilter::province('province_id'),
NepaliAddressFilter::district('district_id'),
NepaliAddressFilter::localBody('municipality_id'),

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();
    }
}

$user = User::find(1);

// Automatic name resolution
$user->province_name;       // "Bagmati Pradesh" (or "बागमती प्रदेश")
$user->district_name;       // "Kathmandu" (or "काठमाडौं")
$user->municipality_name;   // "Kathmandu Metropolitan City" (or "काठमाडौं महानगरपालिका")
$user->local_body_name;     // Alias for municipality_name

// Formatted address strings
$user->address;             // "Kathmandu-4, Kathmandu, Bagmati Pradesh" (via getAddressAttribute)
$user->nepali_address;      // "Kathmandu-4, Kathmandu, Bagmati Pradesh"
$user->full_nepali_address; // Alias for nepali_address

// Resolve names with custom prefix
$user->getProvinceName('billing_');     // "Bagmati Pradesh"
$user->getDistrictName('billing_');     // "Kathmandu"
$user->getLocalBodyName('billing_');    // "Kathmandu Metropolitan City"

// Format address with custom prefix, ward toggle, and separator
$user->getNepaliAddress(
    prefix: 'billing_',
    withWard: true,
    separator: ' - '
);
// "Kathmandu-4 - Kathmandu - Bagmati Pradesh"

// Address without ward
$user->getNepaliAddress('shipping_', withWard: false);
// "Kathmandu, Bagmati Pradesh"

// 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',
];
bash
php artisan vendor:publish --tag="nepali-address-config"
bash
php artisan vendor:publish --tag="nepali-address-translations"
bash
composer analyse