Reading addresses
use RoundlyConsulting\Addresses\Enums\AddressType;
use RoundlyConsulting\Addresses\Facades\Addresses;
$book = Addresses::for($payer);
$book->all(); // Collection<Address>, oldest first
$book->primary(); // primary across all types (or null)
$book->primary(AddressType::Office); // primary of a type
$book->ofType(AddressType::Shipping); // collection of a type
// Model trait shortcuts (they go through the same book):
$payer->addresses; // all addresses (relation)
$payer->getAddressOfType(AddressType::Home); // first of a type
$payer->getAddressOfType(AddressType::Home, onlyPrimary: true); // first primary of a type
$payer->getPrimaryAddressOfType(AddressType::Office); // the same, as a shorthand
$payer->primaryAddress(); // primary across all types
$payer->primaryAddress(AddressType::Office); // primary of a type
$payer->addressesOfType(AddressType::Shipping); // collection of a type
$payer->hasAddresses(); // bool| Method | Returns | Notes |
|---|---|---|
addresses() | MorphMany | The relation; ->addresses gives the loaded collection. |
getAddressOfType($type, $onlyPrimary = false) | ?Address | The first address of a type, oldest first; only the primary one when the flag is true. |
getPrimaryAddressOfType($type) | ?Address | Shorthand for getAddressOfType($type, onlyPrimary: true). |
primaryAddress(?$type = null) | ?Address | The primary address of a type, or the first primary address of any type. |
addressesOfType($type) | Eloquent Collection | Every address of a type. |
hasAddresses() | bool | Whether the owner has at least one address. |
addressBook() | AddressBook | This model’s address book — the same object Addresses::for($model) returns. |
Every read goes through the owner’s address book and is ordered oldest first, so getAddressOfType() returns the oldest address of that type. When an owner can hold several addresses of one type, read the primary one with primaryAddress($type) or list them all with addressesOfType().
Eager loading
Each helper runs its own query. When you render many owners, eager-load the relation once and filter the loaded collection:
// the helpers run a query on every call — for lists, eager-load the relation once
$customers = Customer::with('addresses')->get();
foreach ($customers as $customer) {
$customer->addresses->where('is_primary', true); // filtered in memory, no extra query
}Without the trait
The address book reads the same data for any Eloquent model — Addresses::for($model)->all(), ->primary($type) and ->ofType($type), each ordered oldest first. See The Addresses facade.
Show your open-source love
This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.
More ways to support, including cryptoBy donating, you agree to our donation terms.
Want this built into your product?
We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.