Laravel Addressable 经过 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.
特征
- 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
POINTcolumn 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_addressaccessor built from a template you control in config
Install the package and publish the migration:
作曲家
要求
masterix21/laravel-addressablephp
工匠
供应商:发布
--提供者=
"Masterix21\Addressable\AddressableServiceProvider"
--标签=
"migrations"php
工匠
迁移
Add the appropriate trait to your model.
HasAddresses
gives you a general-purpose
addresses
relation on any model:
使用
Masterix21\Addressable\Models\Concerns\HasAddresses
;班级
店铺
延伸
模型{
使用
HasAddresses
;}商店
->
addAddress
([
'street_address1'
=>
'4 Privet Drive'
,
'zip'
=>
'GU26 6HS'
,
'城市'
=>
'Little Whinging'
,
'状态'
=>
'Surrey'
,
'国家'
=>
'GB'
,]);商店
->
addresses;
// MorphMany of Address models
HasBillingAddresses
和
HasShippingAddresses
each add typed relations and scoped helpers for models that need to distinguish between address types:
使用
Masterix21\Addressable\Models\Concerns\HasBillingAddresses
;使用
Masterix21\Addressable\Models\Concerns\HasShippingAddresses
;班级
用户
延伸
模型{
使用
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:
$用户
->
addBillingAddress
([
'标签'
=>
'Head Office'
,
'street_address1'
=>
'221B Baker Street'
,
'zip'
=>
'NW1 6XE'
,
'城市'
=>
'London'
,
'状态'
=>
'England'
,
'国家'
=>
'GB'
,]);
这
meta
JSON column lets you store anything that doesn't fit the standard fields without touching the schema:
$用户
->
addShippingAddress
([
'street_address1'
=>
'1600 Pennsylvania Ave NW'
,
'城市'
=>
'Washington'
,
'状态'
=>
'DC'
,
'国家'
=>
'US'
,
'元'
=>
[
'电话'
=>
'+1 202 456 1111'
,
'floor'
=>
1
,
'notes'
=>
'Leave at reception'
,],]);$地址
->
meta[
'电话'
];
// '+1 202 456 1111'
这
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回声
$用户
->
billingAddress
->
display_address;// "221B Baker Street - NW1 6XE - London - England - GB"// All shipping addressesforeach
($用户)
->
shippingAddresses
作为
$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:
使用
MatanYadaev\EloquentSpatial\Objects\Point
;$用户
->
addBillingAddress
([
'street_address1'
=>
'10 Downing Street'
,
'城市'
=>
'London'
,
'国家'
=>
'GB'
,
坐标
=>
新的
观点
(
51.5034
,
-
0.1276
,
配置
(
'addressable.srid'
)),]);$origin
=
新的
观点
(
51.5074
,
-
0.1278
,
配置
(
'addressable.srid'
));// Addresses within 3 km地址
::
询问
()
->
withinRadius
($origin,
3_000
)
->
得到
();// Five closest billing addresses地址
::
询问
()
->
计费
()
->
nearest
($origin,
5
)
->
得到
();
Query scopes for
primary()
,
billing()
, 和
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 。







