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

v1.0.0 2025-11-29 11:35 UTC

This package is auto-updated.

Last update: 2025-12-29 11:46:59 UTC


README

License: MIT Laravel PHP Tests

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 Address model attachable to any Eloquent model via a trait
  • Lookup tables for Country and AdministrativeArea with seeders and migrations
  • Postal code normalization for US and Canada via (PostalCodeFormatter) with a clear, nullable contract
  • DB-backed AddressValidator to 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.

๐Ÿ”— Links