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

Using NepalData in Laravel

After installation, Laravel’s package discovery reads Composer’s metadata and registers KodeFarmers\NepalData\NepalDataServiceProvider. The provider binds the core service as a container singleton: facade calls and dependency injection resolve the same instance. Queries return independent filtered selections, so reusing this service does not carry a previous filter into a new call.

No config publishing, application migrations, or package-specific environment variables are required.

Use the facade

Import the facade from KodeFarmers\NepalData\Facades, not the standalone class of the same short name. A local alias keeps the distinction clear:

use KodeFarmers\NepalData\Entities\District;
use KodeFarmers\NepalData\Facades\NepalData as Nepal;
use KodeFarmers\NepalData\Language;

$kaski = Nepal::districts(Language::NEPALI)->findOrFail(District::KASKI);
$ruralNames = $kaski->localLevels()->ruralMunicipalities()->names();

The facade exposes provinces(), districts(), localLevels(), and raw() with the same behavior as the core API. Its PHPDoc supplies the concrete query return types for editor and static-analysis tooling. The package does not register a global facade alias; use the explicit import.

Inject the core service

Type-hint KodeFarmers\NepalData\NepalData in a controller, service, or route. For example, add this to routes/web.php:

use Illuminate\Support\Facades\Route;
use KodeFarmers\NepalData\NepalData;

Route::get('/nepal/provinces', static fn (NepalData $data): array => $data->provinces()->toArray());

A request to /nepal/provinces returns the scalar province rows as JSON. The route is part of your application; NepalData itself registers no routes. To resolve the same instance explicitly in Laravel, use app(\KodeFarmers\NepalData\NepalData::class).

Use Laravel collections

use KodeFarmers\NepalData\Entities\Province;
use KodeFarmers\NepalData\Facades\NepalData as Nepal;

$query = Nepal::districts()->whereProvince(Province::GANDAKI)->orderByName();

$records = collect($query->get());           // Collection of District objects
$rows = collect($query->toArray());          // Collection of arrays
$options = $rows->pluck('name', 'id')->all(); // Names keyed by package district ID

Use get() when you need methods such as province() on the objects. Use toArray() when applying collection operations to scalar fields such as province_id. If you only need names, $query->names() is enough and does not require a collection.

If automatic discovery is disabled

Append \KodeFarmers\NepalData\NepalDataServiceProvider::class to the application’s existing provider list. The default location is bootstrap/providers.php in Laravel 11 or later, or the providers list in config/app.php in Laravel 8 through 10. Keep the rest of the provider list intact.

For standalone PHP, instantiate the core class directly as shown in the quick start. The facade needs a bootstrapped Laravel application.