blamodex / laravel-addresses
Address management package for Laravel applications.
Installs: 39
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/blamodex/laravel-addresses
Requires
- php: ^8.2
- illuminate/config: ^12.0
- illuminate/database: ^12.0
- illuminate/queue: ^12.0
- illuminate/support: ^12.0
Requires (Dev)
- blamodex/laravel-ai-agents: ^1.0
- larastan/larastan: ^3.0
- orchestra/testbench: ^10.0
- phpunit/php-code-coverage: *
- phpunit/phpunit: ^11.5.3
- squizlabs/php_codesniffer: 4.x-dev
This package is auto-updated.
Last update: 2025-12-29 11:46:59 UTC
README
A lightweight Laravel package to manage postal addresses, countries and administrative areas, suitable for attaching addresses to any Eloquent model using polymorphic relationships.
๐ Table of Contents
๐ Features
- Polymorphic
Addressmodel attachable to any Eloquent model via a trait - Lookup tables for
CountryandAdministrativeAreawith seeders and migrations - Postal code normalization for US and Canada via (
PostalCodeFormatter) with a clear, nullable contract - DB-backed
AddressValidatorto validate country, administrative area and postal codes - Service layer (
AddressService) for create/update/delete/list operations - Soft deletes, UUID generation, and test coverage via Orchestra Testbench
๐ฆ Installation
Install the package with Composer:
composer require blamodex/laravel-addresses
Publish the config file:
php artisan vendor:publish --tag=blamodex-address-config
Run the migrations:
php artisan migrate
โ๏ธ Configuration
Configuration lives in config/address.php (empty by default, but you can add options for formatting or validation):
return [ // e.g. 'default_country' => 'CA' ];
๐งฉ Usage
1. Use the Addressable trait on models
For models that can have addresses:
use Blamodex\Address\Traits\Addressable; use Blamodex\Address\Contracts\AddressableInterface; class User extends Model implements AddressableInterface { use Addressable; }
2. Create an address
$user = User::find(1); // Using the trait method $address = $user->createAddress([ 'address_1' => '100 Main St', 'city' => 'Anytown', 'administrative_area_code' => 'CA', 'postal_code' => '90001', 'country_code' => 'US', ]); // Or using the service directly $service = app(\Blamodex\Address\Services\AddressService::class); $address = $service->create($user, $attributes);
3. Update an address
$user->updateAddress($address, ['address_1' => '200 Main St']); // Or via service $service->update($address, ['address_1' => '200 Main St']);
4. Delete an address
$user->deleteAddress($address); // Or via service $service->delete($address);
5. Validation
Use the AddressValidator to check attributes before persisting:
$errors = \Blamodex\Address\Validators\AddressValidator::validate($attributes); // Or throw on error \Blamodex\Address\Validators\AddressValidator::validateOrFail($attributes);
PostalCodeFormatter::format() accepts ?string and returns ?string โ null indicates invalid or unsupported postal code.
๐งช Testing
This package uses Orchestra Testbench and PHPUnit.
Run tests:
composer test
Run tests with code coverage:
composer test:coverage
Check code style:
composer lint
Check code style and fix:
composer lint:fix
Check static analysis (phpstan) and tests as part of CI as configured in the repo.
๐ Project Structure
src/
โโโ Models/
โ โโโ Address.php
โ โโโ Country.php
โ โโโ AdministrativeArea.php
โโโ Services/
โ โโโ AddressService.php
โโโ Traits/
โ โโโ Addressable.php
โโโ Contracts/
โ โโโ AddressableInterface.php
โโโ Validators/
โ โโโ AddressValidator.php
โโโ Utils/
โ โโโ PostalCodeFormatter.php
โโโ config/
โ โโโ address.php
โโโ database/
โโโ migrations/
โโโ seeders/
tests/
โโโ Unit/
โ โโโ AddressServiceTest.php
โ โโโ AddressTest.php
โ โโโ AdministrativeAreaTest.php
โ โโโ CountryTest.php
โ โโโ PostalCodeFormatterTest.php
โ โโโ AddressValidatorTest.php
โโโ Fixtures/
โ โโโ DummyAddressUser.php
โ โโโ DummyAddressCompany.php
โโโ TestCase.php
๐ License
MIT ยฉ Blamodex
๐ค Contributing
Contributions are welcome! Please see CONTRIBUTING.md for details.