百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
B

belongs-to-through

> 编程语言
开源

Laravel Eloquent 的 BelongsToThrough 关系

1.3K stars0 点赞4 次浏览
访问官网GitHub

工具介绍

Laravel Eloquent 的 BelongsToThrough 关系

BelongsToThrough 此种反向版本的 HasManyThrough 允许具有无限中间模型的 BelongsToThrough 关系。 支持 Laravel 5.0+。 安装 composer require staudenmeir/belongs-to-through:"^2.5" 如果您在 Windows 上使用 PowerShell (例如在 VS Code 中),请使用以下命令: composer require staudenmeir/belongs-to-through:"^^^^2.5" 版本 | Laravel | 包 | |:--------|:--------| | 13.x | 2.18 | | 12.x | 2.17 | | 11.x | 2.16 | | 10.x | 2.13 | | 9.x | 2.12 | | 8.x | 2.11 | | 7.x | 2.10 | | 6.x | 2.6 | | 5.x | 2.5 | 使用 - 自定义外键 - 自定义本地键 - 表别名 - 软删除 考虑以下 HasManyThrough 关系: Country → 具有多个 → User → 具有多个 → Post php 类 Country extends Model { public function posts() { return $this-hasManyThrough(Post::class, User::class); } } 在您的模型中使用 BelongsToThrough 特征来定义反向关系: Post → 归属于 → User → 归属于 → Country php 类 Post extends Model { use \Znck\Eloquent\Traits\BelongsToThrough; public function country(): \Znck\Eloquent\Relations\BelongsToThrough { return $this-belongsToThrough(Country::class, User::class); } } 您还可以定义更深层次的关系: Comment → 归属于 → Post → 归属于 → User → 归属于 → Country 以数组形式提供中间模型作为第二个参数,从相关模型(Country)到父模型(Comment): php 类 Comment extends Model { use \Znck\Eloquent\Traits\BelongsToThrough; public function country(): \Znck\Eloquent\Relations\BelongsToThrough { return $this-belongsToThrough(Country::class, [User::class, Post::class]); } } 自定义外键 您可以指定自定义外键作为第五个参数: php 类 Comment extends Model { use \Znck\Eloquent\Traits\BelongsToThrough; public function country(): \Znck\Eloquent\Relations\BelongsToThrough { return $this-belongsToThrough( Country::class, [User::class, Post::class], foreignKeyLookup: [User::class = 'customuserid'] ); } } 自定义本地键 您可以为关系指定自定义本地键: VendorCustomerAddress → 归属于 → VendorCustomer 在 VendorCustomerAddress.vendorcustomerid 中 VendorCustomerAddress → 归属于 → CustomerAd…

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHP

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言