The HasAddresses trait
Add the trait to any Eloquent model that owns addresses — customers, companies, orders, venues. Implementing the Addressable contract is optional, but it lets other code typehint the interface instead of your concrete model:
use Illuminate\Database\Eloquent\Model;
use RoundlyConsulting\Addresses\Contracts\Addressable;
use RoundlyConsulting\Addresses\Traits\HasAddresses;
class Customer extends Model implements Addressable
{
use HasAddresses;
}The relation
addresses() is a MorphMany onto the configured address model, so every Eloquent query method and the package’s scopes are available on it:
use RoundlyConsulting\Addresses\Enums\AddressType;
$customer->addresses; // Collection of Address
$customer->addresses()->count();
$customer->addresses()->primary()->ofType(AddressType::Billing)->get();
$customer->addresses()->inCountry('SK')->latest()->get();What the trait adds
- addresses() — the polymorphic relation.
- newAddress() — start the fluent builder bound to this model.
- addAddress(AddressData $data) and createAddress(...) — create from a DTO or from named arguments.
- getAddressOfType(), getPrimaryAddressOfType(), primaryAddress(), addressesOfType(), hasAddresses() — read.
- setPrimaryAddress(Address $address) — promote one of this model’s addresses, ownership-guarded.
- addressBook() — this model’s AddressBook, the same object Addresses::for($model) returns.
The helpers are sugar over the facade: every one goes through the model’s address book, so host overrides and Addresses::fake() see each call.
Typehint the contract
Addressable declares every trait method except addresses(), createAddress() and addressBook(), so your functions can accept any address owner:
use RoundlyConsulting\Addresses\Address;
use RoundlyConsulting\Addresses\Contracts\Addressable;
use RoundlyConsulting\Addresses\Enums\AddressType;
function shippingAddressFor(Addressable $owner): ?Address
{
return $owner->primaryAddress(AddressType::Shipping)
?? $owner->getAddressOfType(AddressType::Shipping);
}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.