ajcastro/eager-load-pivot-relations
Composer 安装命令:
composer require ajcastro/eager-load-pivot-relations
包简介
Eager load pivot relations for Laravel Eloquent's BelongsToMany relation.
README 文档
README
Eager load pivot relations for Laravel Eloquent's BelongsToMany relation.
Medium Story: https://medium.com/@ajcastro29/laravel-eloquent-eager-load-pivot-relations-dba579f3fd3a
Installation
composer require ajcastro/eager-load-pivot-relations
Usage and Example
There are use-cases where in a pivot model has relations to be eager loaded. Example, in a procurement system, we have the following:
Tables
items
- id
- name
units
- id
- name (pc, box, etc...)
plans (annual procurement plan)
- id
plan_item (pivot for plans and items)
- id
- plan_id
- item_id
- unit_id
Models
class Unit extends \Eloquent { } use AjCastro\EagerLoadPivotRelations\EagerLoadPivotTrait; class Item extends \Eloquent { // Use the trait here to override eloquent builder. // It is used in this model because it is the relation model defined in // Plan::items() relation. use EagerLoadPivotTrait; public function plans() { return $this->belongsToMany('Plan', 'plan_item'); } } class Plan extends \Eloquent { public function items() { return $this->belongsToMany('Item', 'plan_item') ->using('PlanItem') // make sure to include the necessary foreign key in this case the `unit_id` ->withPivot('unit_id', 'qty', 'price'); } } // Pivot model class PlanItem extends \Illuminate\Database\Eloquent\Relations\Pivot { protected $table = 'plan_item'; public function unit() { return $this->belongsTo('Unit'); } }
From the code above, plans and items has Many-to-Many relationship. Each item in a plan has a selected unit, unit of measurement.
It also possible for other scenario that the pivot model will have other many relations.
Eager Loading Pivot Relations
Use keyword pivot in eager loading pivot models. So from the example above, the pivot model PlanItem can eager load the unit relation by doing this:
return Plan::with('items.pivot.unit')->get();
The resulting data structure will be:
You may also access other relations for example:
return Plan::with([
'items.pivot.unit',
'items.pivot.unit.someRelation',
'items.pivot.anotherRelation',
// It is also possible to eager load nested pivot models
'items.pivot.unit.someBelongsToManyRelation.pivot.anotherRelationFromAnotherPivot',
])->get();
Custom Pivot Accessor
You can customize the "pivot accessor", so instead of using the keyword pivot, we can declare it as planItem.
Just chain the as() method in the definition of the BelongsToMany relation.
class Plan extends \Eloquent { public function items() { return $this->belongsToMany('Item', 'plan_item') ->withPivot('unit_id', 'qty', 'price') ->using('PlanItem') ->as('planItem'); } }
Make sure we also use the trait
to our main model which is the Plan model, because the package needs to acess
the belongsToMany relation (items relation) to recognize the used pivot acessor.
use AjCastro\EagerLoadPivotRelations\EagerLoadPivotTrait; class Plan extends \Eloquent { use EagerLoadPivotTrait; }
So instead of using pivot, we can eager load it by defined pivot accessor planItem.
return Plan::with('items.planItem.unit')->get();
$plan = Plan::with('items.planItem.unit'); foreach ($plan->items as $item) { $unit = $item->planItem->unit; echo $unit->name; }
Other Examples and Use-cases
https://github.com/ajcastro/eager-load-pivot-relations-examples
ajcastro/eager-load-pivot-relations 适用场景与选型建议
ajcastro/eager-load-pivot-relations 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.86M 次下载、GitHub Stars 达 245, 最近一次更新时间为 2019 年 04 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ajcastro/eager-load-pivot-relations 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ajcastro/eager-load-pivot-relations 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.86M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 245
- 点击次数: 28
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-04-23
