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

Relationships and Output

Each local level belongs to a district, and each district belongs to a province in the bundled hierarchy:

Province
  -> District
     -> LocalLevel

Going from a parent to its children returns a query you can filter further. Going from a record to its parent returns that record directly. The parent’s language matches the record you started from.

Traverse the hierarchy

The examples below use $data from the quick start:

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

$gandaki = $data->provinces(Language::NEPALI)->findOrFail(Province::GANDAKI);
$districts = $gandaki->districts()->get();
$ruralLevels = $gandaki->localLevels()->ruralMunicipalities()->get();

$kaski = $gandaki->districts()->findOrFail(District::KASKI);
$provinceName = $kaski->province()->name(); // गण्डकी
$provinceId = $kaski->provinceId();        // Province::GANDAKI

$localLevel = $kaski->localLevels()->findByName('पोखरा');
if ($localLevel !== null) {
    $district = $localLevel->district();   // कास्की
    $province = $localLevel->province();   // गण्डकी
}

Use provinceId() or districtId() when you only need the integer parent ID. They expose the same values as province()->id() or district()->id(), respectively.

Prepare data for a form

get() and all() return zero-indexed PHP arrays of read-only objects. Use toArray() for scalar rows that work with helpers such as array_column():

use KodeFarmers\NepalData\Entities\Province;

$query = $data->districts()->whereProvince(Province::GANDAKI)->orderByName();
$options = array_column($query->toArray(), 'name', 'id'); // Names keyed by district ID

For a dependent select, take the validated province ID from the first field, filter districts with whereProvince(), and then filter local levels with whereDistrict() after the user chooses a district. Use the same language across all three lists. See Troubleshooting for an example of validating incoming IDs.

Serialize records and queries

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

$district = $data->districts(Language::NEPALI)->findOrFail(District::KASKI);
$row = $district->toArray();
$json = json_encode($district->localLevels(), JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);

With json_encode(), a record produces one JSON object and a query produces a JSON array of objects, or [] if empty. Both implement JsonSerializable, and jsonSerialize() returns the same PHP array as toArray(). A list returned by get() can also be JSON-encoded because each item is serializable. JSON_UNESCAPED_UNICODE makes Nepali text readable in the output; JSON_THROW_ON_ERROR reports encoding errors as exceptions.

EntityFields in toArray() and JSON
Provinceid, name, language
Districtid, name, language, province_id
LocalLevelid, name, language, province_id, district_id, type, type_label

IDs are integers; the other fields are strings. Parent records and child queries are not included automatically. If your client needs a nested hierarchy or just a handful of fields, assemble that response explicitly from the records and relationships you need. Use original arrays if the client relies on the exact v1 shape instead.

Changing an array returned by toArray() or raw() does not change the package data. Replacing items in a list returned by get() likewise does not affect the query or later calls.