mdgascar / malagasy-address-laravel
Laravel validation rule, Blade directive and Eloquent cast for the codes of the regions, districts, communes and fokontany of Madagascar.
Package info
github.com/mdgascar/malagasy-address-laravel
pkg:composer/mdgascar/malagasy-address-laravel
Requires
- php: >=8.3
- illuminate/contracts: ^12.0 || ^13.0
- illuminate/database: ^12.0 || ^13.0
- illuminate/support: ^12.0 || ^13.0
- illuminate/validation: ^12.0 || ^13.0
- mdgascar/malagasy-address: ^0.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.95
- orchestra/testbench: ^10.0 || ^11.0
- phpstan/phpstan: ^2.3
- phpstan/phpstan-strict-rules: ^2.1
- phpunit/phpunit: ^11.5 || ^12.5 || ^13.4
- povils/phpmnd: ^3.6
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
malagasy-address-laravel
A Laravel validation rule, Blade directive and Eloquent cast for the codes of the regions, districts, communes and fokontany of Madagascar, on top of mdgascar/malagasy-address. Laravel 12 and 13, PHP 8.3+.
Docs: mdgascar.github.io/malagasy-docs/docs/places.
composer require mdgascar/malagasy-address-laravel
The service provider registers itself.
use Mdgascar\MalagasyAddress\Laravel\Rules\MalagasyPlace; use Mdgascar\MalagasyAddress\Places\PlaceLevel; $request->validate([ 'region' => ['required', 'mg_place:region'], 'commune' => ['required', new MalagasyPlace(PlaceLevel::Commune)], 'place' => ['required', 'mg_place'], ]);
mg_place accepts any level; add :region, :district, :commune or :fokontany to accept one.
Add a code after it to accept only the places below that code: mg_place:commune,MG11102 takes the
communes of one district, mg_place:,MG11102 any level below it. The rule object takes the same:
new MalagasyPlace(PlaceLevel::Commune, 'MG11102'). The rule checks that the code exists in the list
of divisions.
The @mgPlace directive prints a place with its parents; it takes a code or a Place, and prints
nothing for null:
@mgPlace($user->commune) {{-- Ankadinandriana, Antananarivo Avaradrano, Analamanga --}}
The AsPlace cast stores a place as its code and reads it back as a Place with its parents. An
unknown code throws before it reaches the database, so validate input with mg_place first:
protected function casts(): array { return ['commune' => AsPlace::class]; }
Messages come in English and French. Publish them with
php artisan vendor:publish --tag=malagasy-address-lang to change the wording.
Data and licence
The code is MIT. The place data comes from the OCHA COD-AB for Madagascar (CC BY-IGO): see the
NOTICE of malagasy-address-php.
Contributing
See CONTRIBUTING.md.
