Troubleshooting
Composer cannot install ^2.0
Check the published versions, the PHP version used by Composer, and the JSON extension. php -v and php -m report your current CLI runtime. After installation, composer show kodefarmers/nepaldata reports the installed version and composer check-platform-reqs checks the actual PHP environment rather than a simulated config.platform value.
If the installed package is v1, its calls return a different shape. See Upgrading from v1. For testing unpublished v2 code in a separate Composer project, use the local source checkout instructions.
Laravel cannot resolve the facade
- If calling a method statically, import
KodeFarmers\NepalData\Facades\NepalData, notKodeFarmers\NepalData\NepalData. For dependency injection, type-hint the latter. - Check whether the application disables package discovery. If so, register the provider in the existing provider list. If discovery is enabled,
php artisan package:discoverrebuilds the package manifest. - The facade needs an initialized Laravel application. In a plain PHP script with only Composer’s autoloader, create
new \KodeFarmers\NepalData\NepalData()instead.
A lookup returns nothing or throws
Confirm the selected language, the type of record, and the filters that are already applied. For example, $data->districts()->whereProvince(Province::KARNALI)->find(District::KASKI) returns null because Kaski is in Gandaki. An absent positive ID is different from an invalid ID such as zero.
If findByName() throws DomainException, several selected records have the same name. Filter by district or province first, or use whereName($name)->get() to show all candidates. If a lookup misses a familiar spelling, check the returned source names or use search() with a substring in the selected language. The package does not translate, strip punctuation, or apply locale-aware collation.
findOrFail() throws PHP’s OutOfBoundsException; it is not an Eloquent lookup and does not automatically turn a missing record into an HTTP 404. In an application action, handle null from find() or map the exception to your own response as appropriate. The error table lists the remaining cases.
Validate input before querying
HTML form fields typically arrive as strings. Validate IDs and language codes before passing them to the package. This standalone example validates a positive integer and then checks that it is an actual province ID:
use KodeFarmers\NepalData\Entities\Province;
$submittedProvince = (string) Province::GANDAKI; // Replace with the submitted scalar value.
$provinceId = filter_var($submittedProvince, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
if ($provinceId === false) {
throw new InvalidArgumentException('Select a valid province ID.');
}
$province = $data->provinces()->find($provinceId);
if ($province === null) {
throw new OutOfBoundsException('The selected province does not exist.');
}
$districtOptions = array_column($province->districts()->toArray(), 'name', 'id');
If both a province and a district are submitted, validate the district ID by looking it up on $province->districts() so the relationship is checked too. In Laravel, use request validation for integer with min:1 and an allowlist of en,ne for a submitted language, then cast the validated ID to int before querying.
Output is empty or does not match v1
Iterate over $query->get() or $query->all() rather than the query object itself. toArray() and JSON output contain scalar fields, not loaded child records. For exact v1 nested arrays, use raw()->all() in the desired language. For original flat names, use the corresponding raw view rather than trying to reconstruct it from typed records.