Query scopes
use RoundlyConsulting\Addresses\Address;
use RoundlyConsulting\Addresses\Enums\AddressType;
Address::query()->primary()->get();
Address::query()->ofType(AddressType::Billing)->get();
Address::query()->inCountry('SK')->get();
// combine them, on the model or on any owner's relation
Address::query()->primary()->ofType(AddressType::Shipping)->inCountry('sk')->count();
$company->addresses()->inCountry('US')->ofType(AddressType::Office)->get();
// soft-deleted rows are excluded unless you ask for them
Address::withTrashed()->inCountry('SK')->get();| Scope | Filters to |
|---|---|
primary() | is_primary = true |
ofType(AddressType $type) | type = the enum’s value |
inCountry(string $iso) | country_iso = the trimmed, upper-cased code; compared case-insensitively with normalise_country off |
inCountry() matches codes the way they were stored: with normalise_country on it trims and upper-cases its argument (' sk ' finds SK); with it off it compares case-insensitively ('SK' finds a verbatim sk). Alpha-2 and alpha-3 codes are never mapped onto each other — SK never matches SVK — so store one style consistently.
The model uses soft deletes, so every query excludes deleted addresses unless you add withTrashed() or onlyTrashed(). With a custom model, call the scopes on your own class — they are inherited.
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.