ferdirn/laravel-id-provinces
Laravel package that adds Indonesian provinces data to your app. Provides migration and seeder to create and populate a provinces table with province name, country code, capital, and area (km²). Includes service provider and facade.
Installation
composer require ferdirn/laravel-id-provinces:dev-master
Update dependencies:
composer update
Register Provider & Facade
Add to config/app.php under providers:
Ferdirn\Provinces\ProvincesServiceProvider::class,
Add to aliases:
'Provinces' => Ferdirn\Provinces\ProvincesFacade::class,
Publish Configuration (Optional)
php artisan vendor:publish --provider="Ferdirn\Provinces\ProvincesServiceProvider"
Config file will be published to config/provinces.php.
First Use Case Fetch all provinces in a controller or service:
use Provinces;
$provinces = Provinces::all();
return response()->json($provinces);
Or use the facade directly:
$jakarta = Provinces::findByName('DKI Jakarta');
Fetching Provinces
Provinces::all() returns a collection of all provinces with attributes: id, name, country_code, capital, and area.Provinces::find($id).Provinces::findByName('Name') (case-insensitive).Integration with Eloquent Models
Add a province_id foreign key to a model (e.g., User, Address) and use the facade to validate or fetch province data:
use Provinces;
public function store(Request $request)
{
$validated = $request->validate([
'province_id' => 'required|exists:provinces,id',
]);
$province = Provinces::find($validated['province_id']);
// Use $province->name, $province->capital, etc.
}
Dynamic Data in Views Cache province data in a service provider for reuse:
// In a service provider's boot method
view()->share('provinces', Provinces::all()->pluck('name', 'id'));
Then use in Blade:
<select name="province_id">
@foreach ($provinces as $id => $name)
<option value="{{ $id }}">{{ $name }}</option>
@endforeach
</select>
API Responses Return province data in API responses:
return response()->json([
'province' => Provinces::find($request->province_id),
]);
Localization Extend the package by publishing the config and customizing province names/capitals for localized apps:
// config/provinces.php
'custom_names' => [
'DKI Jakarta' => 'Jakarta Raya',
],
Dev-Master Dependency
The package is in dev-master state. Pin the version in composer.json to avoid unexpected updates:
"ferdirn/laravel-id-provinces": "dev-master as 1.0.0"
Case Sensitivity in findByName
The findByName method is case-insensitive but may return unexpected results if the input has extra spaces or typos. Trim and sanitize inputs:
$province = Provinces::findByName(trim($request->province_name));
Database Migration Conflicts
If you manually create a provinces table, the package’s migrations will fail. Either:
'migrate' => false in config/provinces.php.Facade vs. Service Container
Avoid instantiating the Provinces class directly. Always use the facade (Provinces::) or resolve via the container (app(Provinces::class)) for consistency.
Check Published Config
If provinces aren’t loading, verify config/provinces.php exists and is correctly configured. Default values may override custom settings.
Data Integrity Validate province data by dumping a sample:
dd(Provinces::all()->first());
Ensure id, name, country_code, capital, and area are present.
Service Provider Binding
If the facade throws Class not found, ensure the ProvincesServiceProvider is registered in config/app.php after Laravel’s core providers.
Custom Province Attributes
Extend the Province model (published at app/Province.php after publishing) to add methods:
// app/Province.php
public function isCapital()
{
return $this->capital === $this->name;
}
Override Data Source Replace the default provinces data by binding a custom collection in a service provider:
$this->app->bind('provinces', function () {
return collect([/* custom provinces */]);
});
Add Cities or Districts
Pair this package with ferdirn/laravel-id-cities (if available) or create a similar package for cities/districts to build a hierarchical location system.
Caching Cache province data in a service provider to reduce database queries:
$this->app->singleton('provinces', function () {
return Cache::remember('provinces', now()->addDays(30), function () {
return Provinces::all();
});
});
Testing Mock the facade in tests:
Provinces::shouldReceive('findByName')->once()->andReturn($mockProvince);
How can I help you explore Laravel packages today?