Package Data | |
---|---|
Maintainer Username: | inxilpro |
Package Create Date: | 2016-07-21 |
Package Last Update: | 2024-07-22 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2025-02-20 03:15:51 |
Package Statistics | |
---|---|
Total Downloads: | 251,558 |
Monthly Downloads: | 4,319 |
Daily Downloads: | 141 |
Total Stars: | 65 |
Total Watchers: | 5 |
Total Forks: | 14 |
Total Open Issues: | 2 |
Laravel package providing addressing functionality
First, install the composer package:
composer require galahad/laravel-addressing
In config/app.php
add the Service Provider:
'providers' => [
// ...
Galahad\LaravelAddressing\ServiceProvider::class
],
And add the Addressing
alias in the same file:
'aliases' => [
// ...
'Addressing' => Galahad\LaravelAddressing\AddressFacade::class
],
$country = Addressing::country('US');
echo $country->getName(); // United States
echo Addressing::country('US')->administrativeArea('AL')->getName(); // Alabama
$administrativeAreas = Addressing::country('BR')->administrativeAreas();
foreach ($administrativeAreas as $administrativeArea) {
echo sprint("[%s]: %s\n", $administrativeArea->getCode(), $administrativeArea->getName());
}
You can use some custom validators in your Laravel app:
You can use country_code
and country_name
validators:
$this->validate($request, [
'country' => 'required|country_code',
]);
$this->validate($request, [
'country' => 'required|country_name',
]);
You can use administrative_area_code
, administrative_area_name
or administrative_area
(verifies both code
and name
):
$this->validate($request, [
'state' => 'required|administrative_area_code:country_field',
]);
$this->validate($request, [
'state' => 'required|administrative_area_name:country_field',
]);
$this->validate($request, [
'state' => 'required|administrative_area:country_field', // verifies first code and after name
]);
You can check if the postal code starts with the correct pattern using postal_code
validator:
$this->validate($request, [
'postal_code' => 'required|postal_code:country_field,administrative_area_field',
]);
You can also get Countries and Administrative Areas (states) in JSON
format:
// GET /galahad/addressing/countries
{
"label": "Countries",
"options": {
"AF": "Afghanistan",
"**": "*******",
"ZW": "Zimbabwe"
}
}
// If error
{
"error": true,
"message": "Could not get countries"
}
// GET /galahad/addressing/US/adminstrative-areas
{
"label": "State",
"expected_length": 2,
"country": "US",
"options": {
"AL": "Alabama",
"**": "*******",
"WY": "Wyoming"
}
}
You can get the countries list using a custom locale:
GET /galahad/addressing/countries?locale=pt
By default the routes returning the JSON responses are prefixed with galahad/addressing
. If you would like to change this, you need to publish the configuration file using php artisan vendor:publish --provider="Galahad\LaravelAddressing\ServiceProvider"
. This will create a config file (addressing.php
) in your config
directory with:
<?php
return [
'route' => [
'prefix' => 'countries' // change this to whatever you'd like
],
];
Special thanks to Commerce Guys for their amazing addressing and intl packages, which this project relies heavily on.