Laravel Cloud 来了!为 Laravel 应用提供零配置的托管基础​​设施。 立即部署

Attach Addresses to Any Eloquent Model with Laravel Addressable

发布日期 经过

Attach Addresses to Any Eloquent Model with Laravel Addressable image

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 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:

作曲家 要求 masterix21/laravel-addressable
php 工匠 供应商:发布 --提供者= "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

HasBillingAddressesHasShippingAddresses 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 addresses
foreach ($用户) -> 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

Yannick Lyn Fatt 的照片

Laravel News 的特约撰稿人和全栈 Web 开发人员。

归档于:
立方体

Laravel 时事通讯

加入超过 4 万名开发者的行列,不错过任何新的技巧、教程等内容。

图像
Laravel 云

轻松创建和管理服务器,并在几秒钟内部署 Laravel 应用程序。

访问 Laravel Cloud
Tinkerwell 徽标

廷克威尔

Laravel 开发者必备的代码运行器。可在本地和生产环境中体验 AI、自动补全和即时反馈功能。

廷克威尔
Laravel Cloud 标志

Laravel 云

轻松创建和管理服务器,并在几秒钟内部署 Laravel 应用程序。

Laravel 云
Kirschbaum 标志

樱桃树

提供创新和稳定性,确保您的Web应用程序取得成功。

樱桃树
Shift 标志

转移

还在运行旧版本的 Laravel?立即实现 Laravel 自动升级和代码现代化,让您的应用程序保持最新状态。

转移
PhpStorm 标志

PhpStorm

首选的 PHP IDE,对 Laravel 及其生态系统提供广泛的开箱即用支持。

PhpStorm
SaaSykit:Laravel SaaS 入门套件徽标

SaaSykit:Laravel SaaS 入门套件

SaaSykit 是一个多租户 Laravel SaaS 入门套件,包含运行现代 SaaS 所需的所有功能,例如支付、美观的结账界面、管理面板、用户仪表盘、身份验证、现成组件、统计数据、博客、文档等等。

SaaSykit:Laravel SaaS 入门套件
鱼叉:新一代时间跟踪和发票标志

Harpoon:新一代时间跟踪和发票系统

新一代时间跟踪和计费软件,帮助您的机构规划和预测盈利的未来。

Harpoon:新一代时间跟踪和发票系统
几天内即可获得 Laravel 代码审查徽标的专家指导

几天内即可获得 Laravel 代码审查方面的专家指导

专家级代码审查!两位拥有 10 年以上 Laravel 开发经验的开发者将为您提供清晰、实用的反馈,帮助团队构建更优质的应用程序。

几天内即可获得 Laravel 代码审查方面的专家指导
了解 Softtech 的标志

了解软科技

Acquaint Softtech 提供 AI 就绪的 Laravel 开发人员,48 小时内即可上手,每月费用为 3000 美元,没有冗长的销售流程,并提供 100% 退款保证。

了解软科技
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
Lucky Media 标志

幸运传媒

Get Lucky Now——拥有十余年经验的 Laravel 开发理想之选!

幸运传媒
Generate HTML Password Rules Attribute in Laravel 13.9.0 image

Generate HTML Password Rules Attribute in Laravel 13.9.0

阅读文章
DHH Joins Laravel Live Denmark 2026 for Fireside Chat with Taylor Otwell image

DHH Joins Laravel Live Denmark 2026 for Fireside Chat with Taylor Otwell

阅读文章
Model-Based Scheduling for Laravel with Cadence image

Model-Based Scheduling for Laravel with Cadence

阅读文章
Laravel's AI SDK adds sub-agents image

Laravel's AI SDK adds sub-agents

阅读文章
Laravel Introduces First-Party Passkey Authentication Support image

Laravel Introduces First-Party Passkey Authentication Support

阅读文章
Scrollbar Styling and Container Size Utilities in Tailwind CSS v4.3.0 image

Scrollbar Styling and Container Size Utilities in Tailwind CSS v4.3.0

阅读文章