Search by

mdgascar / malagasy-address-laravel

johan-mickael

Laravel validation rule, Blade directive and Eloquent cast for the codes of the regions, districts, communes and fokontany of Madagascar.

v0.1.0 2026-12-04 00:52 UTC

This package is auto-updated.

Last update: 2026-12-04 00:55:36 UTC


README

md.gascar

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.

License

MIT