Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Quick Start

The standalone entry point is KodeFarmers\NepalData\NepalData. Create one instance, choose a dataset and language, then retrieve the records you need. In Laravel, the same service can be injected, or you can call the facade.

Run your first lookup

After installing NepalData, save this as nepaldata-example.php next to your project’s vendor/ directory:

<?php

declare(strict_types=1);

use KodeFarmers\NepalData\Entities\District;
use KodeFarmers\NepalData\Entities\Province;
use KodeFarmers\NepalData\Language;
use KodeFarmers\NepalData\NepalData;

require __DIR__ . '/vendor/autoload.php';

$data = new NepalData();

$karnali = $data->provinces()->findOrFail(Province::KARNALI);
echo $karnali->name(), ': ', $karnali->districts()->count(), ' districts', PHP_EOL;

$kaski = $data->districts(Language::NEPALI)->findOrFail(District::KASKI);
echo $kaski->name(), ' -> ', $kaski->province()->name(), PHP_EOL;

$provinceNames = $data->provinces(Language::NEPALI)->orderByName()->names();
echo implode(', ', $provinceNames), PHP_EOL;
php nepaldata-example.php

The first two lines are Karnali: 10 districts and कास्की -> गण्डकी. The last line lists the seven province names from the Nepali snapshot. orderByName() uses bytewise UTF-8 order after ASCII case folding; it does not implement Nepali collation.

Choose the result you want

From that same $data instance:

use KodeFarmers\NepalData\Entities\District;

$query = $data->localLevels()->whereDistrict(District::KASKI)->ruralMunicipalities();

$records = $query->get();     // list<LocalLevel>
$names = $query->names();     // list<string>, in query order
$ids = $query->ids();         // list<int>, in query order
$rows = $query->toArray();    // scalar arrays, handy for JSON and form options
$count = $query->count();     // number of matching records

all() and get() return the same result for a given query. For one record, use find() to handle an absent ID with null, or findOrFail() if absence should raise an exception. first() returns the first match in the current order, or null when nothing matches.

Next: filter and search the catalogs, navigate relationships, or look up a named constant.