Laravel 11 – 13
PHP 8.3+
The project is in a healthy, maintained state
Manage address in laravel
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

pestphp/pest-plugin-arch
^3.0
phpstan/extension-installer
^1.1
phpstan/phpstan-deprecation-rules
^1.0
phpstan/phpstan-phpunit
^1.0

Runtime

 Project Readme

Manage addresses in Laravel

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Polymorphic address management for any Eloquent model. Supports multiple addresses per model with flag-based retrieval (primary, billing, shipping) and country code validation against a seeded countries table.

Installation

composer require centrex/laravel-addresses
php artisan vendor:publish --tag="laravel-addresses-migrations"
php artisan migrate
php artisan db:seed --class="Centrex\Addresses\Database\Seeders\CountrySeeder"

Usage

1. Add the trait to your model

use Centrex\Addresses\Traits\HasAddresses;

class Customer extends Model
{
    use HasAddresses;
}

2. Add an address

The country field must be a valid ISO 3166-1 alpha-2 code present in the seeded countries table.

$customer->addAddress([
    'country_code' => 'BD',
    'city'         => 'Dhaka',
    'state'        => 'Dhaka Division',
    'post_code'    => '1000',
    'street'       => '123 Main Street',
    'is_primary'   => true,
    'is_billing'   => true,
]);

3. Retrieve addresses

// All addresses
$customer->addresses;

// Check if any addresses exist
$customer->hasAddresses();

// Get by flag (primary | billing | shipping)
$customer->getAddress('billing');    // returns flagged address or falls back
$customer->getAddress('shipping', strict: true);  // only flagged, no fallback

// Latest address (no flag filter)
$customer->getAddress();

4. Update and delete

$address = $customer->addresses->first();

$customer->updateAddress($address, ['city' => 'Chittagong']);

$customer->deleteAddress($address);

// Remove all
$customer->flushAddresses();

Deprecated helpers (use getAddress() instead)

$customer->getPrimaryAddress();
$customer->getBillingAddress();
$customer->getShippingAddress();

Testing

composer test        # full suite
composer test:unit   # pest only
composer test:types  # phpstan
composer lint        # pint

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.