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

Queries and Filters

The examples in this chapter use $data, the core instance from the quick start. Start with provinces(), districts(), or localLevels(). Each entry point returns a query, so you can add filters before asking for records, names, IDs, or a count.

Select by ID or relationship

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

$provinceNames = $data->provinces()->whereId(Province::GANDAKI, Province::KARNALI)->names();

$districtNames = $data->districts()
    ->whereProvince(Province::GANDAKI, Province::KARNALI)
    ->whereId(District::KASKI, District::KATHMANDU, District::HUMLA)
    ->names();

Here $districtNames is ['Kaski', 'Humla']. One filter call matches any value you pass. Chaining filters requires all those conditions to match. Kathmandu is excluded because it is in Bagmati. Repeating an ID does not duplicate the result. An unknown positive ID adds no matches and does not affect the other supplied IDs.

whereProvince() works on district and local level queries. whereDistrict() works on local level queries. A province or district record can also start the corresponding query.

Filter local level classifications

use KodeFarmers\NepalData\Entities\District;
use KodeFarmers\NepalData\LocalLevelType;

$selection = $data->localLevels()->whereDistrict(District::KASKI, District::HUMLA);

$ruralNames = $selection->ruralMunicipalities()->orderByName()->names();
$urbanNames = $selection->municipalities()->orderByName()->names();
$ordinaryMunicipalities = $selection->whereType(LocalLevelType::MUNICIPALITY)->get();

municipalities() includes metropolitan cities, sub-metropolitan cities, and municipalities. Use whereType(LocalLevelType::MUNICIPALITY) for the ordinary municipality classification by itself. Pass more than one classification to whereType() when you want a particular combination. The accepted keys and the original abbreviations are listed in the API reference.

Each filter and orderByName() returns a new query. In the example, the rural and urban filters do not alter $selection or each other. Reuse the same base query for counts, option lists, or alternative filters.

Search and look up names

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

$kaski = $data->districts()->findByName('kaski'); // exact match, ignoring ASCII case
$nepaliMatches = $data->localLevels(Language::NEPALI)->search('फुङ')->names();

$allMadiRecords = $data->localLevels()->whereName('Madi')->get();
$madiInChitwan = $data->localLevels()
    ->whereDistrict(District::CHITWAN)
    ->findByName('Madi');
  • search() matches a substring in the current language.
  • whereName() keeps every exact match, including repeated names in different districts.
  • findByName() returns the only exact match or null; it throws DomainException when there is more than one. Narrow the query first, as in the Chitwan example.

Both exact and substring matching ignore ASCII letter case. Spacing, punctuation, and non-ASCII text are compared as written. For example, the English record Taplejung(Phungling) can be found with search('phungling'); the Nepali query needs Nepali text. Search does not translate, transliterate, or normalize Unicode.

Use a custom filter

Pass a predicate that accepts the query’s record type and returns a boolean:

use KodeFarmers\NepalData\Entities\District;
use KodeFarmers\NepalData\Entities\LocalLevel;

$selected = $data->localLevels()
    ->whereDistrict(District::KASKI)
    ->filter(static fn (LocalLevel $level): bool => in_array($level->name(), ['Annapurna', 'Pokhara'], true))
    ->orderByName()
    ->names();

The predicate runs only on the current selection. Exceptions from the predicate reach the caller; the original query remains unchanged.

Queries keep nested source order by default. orderByName() sorts ascending with ASCII case folding and bytewise UTF-8 comparison; ties use ascending ID. Further filters preserve the resulting order.

Handle missing records

find() and findOrFail() respect all current filters. find() returns null for an absent positive ID. findOrFail() raises OutOfBoundsException for the same miss. Invalid inputs, such as ID zero, an unsupported language or classification, or a blank search term, raise InvalidArgumentException instead. See Errors and Empty Results for the full contract.