Get expert guidance in a few days with a Laravel code review

Attach Addresses to Any Eloquent Model with Laravel Addressable

Published on by

Attach Addresses to Any Eloquent Model with Laravel Addressable image

Laravel Addressable by Luca Longo gives any Eloquent model a polymorphic addresses relation, dedicated billing and shipping traits, a free-form JSON meta column, and geospatial distance queries — all wired up through a single migration and a trait.

Features

  • Polymorphic by default: attach addresses to any model without extra join tables
  • Billing and shipping traits: shorthand relations and helper methods scoped per address type
  • Primary address toggling: markPrimary() / unmarkPrimary() with scoped events dispatched on change
  • Geospatial support: store a POINT column and query by radius, distance, or nearest neighbors (requires MySQL 8+, MariaDB 10.5+, or PostgreSQL with PostGIS)
  • JSON meta column: attach arbitrary extra data (phone, floor, delivery notes) to any address
  • Configurable display format: a display_address accessor built from a template you control in config

Install the package and publish the migration:

composer require masterix21/laravel-addressable
 
php artisan vendor:publish --provider="Masterix21\Addressable\AddressableServiceProvider" --tag="migrations"
 
php artisan migrate

Add the appropriate trait to your model. HasAddresses gives you a general-purpose addresses relation on any model:

use Masterix21\Addressable\Models\Concerns\HasAddresses;
 
class Store extends Model
{
use HasAddresses;
}
 
$store->addAddress([
'street_address1' => '4 Privet Drive',
'zip' => 'GU26 6HS',
'city' => 'Little Whinging',
'state' => 'Surrey',
'country' => 'GB',
]);
 
$store->addresses; // MorphMany of Address models

HasBillingAddresses and HasShippingAddresses each add typed relations and scoped helpers for models that need to distinguish between address types:

use Masterix21\Addressable\Models\Concerns\HasBillingAddresses;
use Masterix21\Addressable\Models\Concerns\HasShippingAddresses;
 
class User extends Model
{
use HasBillingAddresses, HasShippingAddresses;
}

With those traits in place, you get billingAddress (primary, MorphOne), billingAddresses (all, MorphMany), and the shipping equivalents. Adding an address is a single method call:

$user->addBillingAddress([
'label' => 'Head Office',
'street_address1' => '221B Baker Street',
'zip' => 'NW1 6XE',
'city' => 'London',
'state' => 'England',
'country' => 'GB',
]);

The meta JSON column lets you store anything that doesn't fit the standard fields without touching the schema:

$user->addShippingAddress([
'street_address1' => '1600 Pennsylvania Ave NW',
'city' => 'Washington',
'state' => 'DC',
'country' => 'US',
'meta' => [
'phone' => '+1 202 456 1111',
'floor' => 1,
'notes' => 'Leave at reception',
],
]);
 
$address->meta['phone']; // '+1 202 456 1111'

The display_address accessor formats an address using a template from config/addressable.php. The default produces strings like "1600 Pennsylvania Ave NW - 20500 - Washington - DC - US", and you can swap the template for anything your UI expects.

// Single address
echo $user->billingAddress->display_address;
// "221B Baker Street - NW1 6XE - London - England - GB"
 
// All shipping addresses
foreach ($user->shippingAddresses as $address) {
echo $address->display_address;
}

For applications that need location-aware queries, the package stores coordinates as a spatial POINT column (backed by matanyadaev/laravel-eloquent-spatial) and adds scopes for radius filtering and nearest-neighbour sorting:

use MatanYadaev\EloquentSpatial\Objects\Point;
 
$user->addBillingAddress([
'street_address1' => '10 Downing Street',
'city' => 'London',
'country' => 'GB',
'coordinates' => new Point(51.5034, -0.1276, config('addressable.srid')),
]);
 
$origin = new Point(51.5074, -0.1278, config('addressable.srid'));
 
// Addresses within 3 km
Address::query()->withinRadius($origin, 3_000)->get();
 
// Five closest billing addresses
Address::query()->billing()->nearest($origin, 5)->get();

Query scopes for primary(), billing(), and shipping() are fully composable, so you can mix them freely — Address::query()->billing()->primary()->first() does exactly what it says.

You can learn more and view the source code on GitHub.

Yannick Lyn Fatt photo

Staff Writer at Laravel News and Full stack web developer.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
SerpApi logo

SerpApi

Access real-time search engine results through a simple API—no more scraping headaches! Use it for AI applications, SEO tools, product research, travel information, and more

SerpApi
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review

The latest

View all →
Generate Livewire Skeleton Placeholders Automatically with Wirebones image

Generate Livewire Skeleton Placeholders Automatically with Wirebones

Read article
Laravel ClickHouse: A Full-Featured ClickHouse Driver for Laravel image

Laravel ClickHouse: A Full-Featured ClickHouse Driver for Laravel

Read article
Laravel Installer Now Returns JSON When Running Inside an AI Agent image

Laravel Installer Now Returns JSON When Running Inside an AI Agent

Read article
Queue-Wide Inspection Methods in Laravel 13.8.0 image

Queue-Wide Inspection Methods in Laravel 13.8.0

Read article
Verifiable Audit Logging with Laravel Chronicle image

Verifiable Audit Logging with Laravel Chronicle

Read article
Ship AI with Laravel: Search Entire PDFs with Zero Search Logic image

Ship AI with Laravel: Search Entire PDFs with Zero Search Logic

Read article