abdelhamiderrahmouni/laravel-pivot-relations-eager-loading
Composer 安装命令:
composer require abdelhamiderrahmouni/laravel-pivot-relations-eager-loading
包简介
A Laravel package that enables eager loading of relationships on pivot tables for BelongsToMany and MorphToMany relationships.
关键字:
README 文档
README
A Laravel package that enables eager loading of relationships on pivot tables for BelongsToMany and MorphToMany relationships.
Problem
Laravel doesn't natively support eager loading relationships defined on custom pivot models. When you try to eager load pivot relationships using with('roles.pivot.assignedBy'), you get:
Illuminate\Database\Eloquent\RelationNotFoundException: Call to undefined relationship [pivot] on model [App\Models\Role].
Solution
This package provides custom relationship classes that extend Laravel's BelongsToMany and MorphToMany relationships, adding support for eager loading pivot relationships natively using ->with('roles.pivot.createdBy') as well as via a withPivotRelations('createdBy') method.
This package supports the get(), lazy(), cursor(), and chunk() methods.
Installation
composer require abdelhamiderrahmouni/laravel-pivot-relations-eager-loading
Usage
1. Create a Custom Pivot Model
Make sure to define your relationships on your custom pivot model:
<?php declare(strict_types=1); namespace App\Models; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\Pivot; class UserSkill extends Pivot { protected $table = 'user_skill'; public function scale(): BelongsTo { return $this->belongsTo(Scale::class); } }
2. Add the Trait to Your Models
<?php declare(strict_types=1); namespace App\Models; use LaravelPivotRelationsEagerLoading\Concerns\WithPivotRelationsLoading; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Model; class User extends Model { use WithPivotRelationsLoading; public function skills(): BelongsToMany { return $this->belongsToMany(Skill::class, 'user_skill') ->using(UserSkill::class) ->withTimestamps() ->withPivot(['scale_id']); } }
$users = User::query()->with('skills.pivot.scale')->get(); foreach ($users as $user) { foreach ($user->skills as $skill) { // The scale relationship is already loaded - no N+1 queries! $scale = $skill->pivot->scale; } }
If you prefer to always eager load a pivot relationship with your relationship definition, you can use the withPivotRelations() method:
<?php declare(strict_types=1); namespace App\Models; use LaravelPivotRelationsEagerLoading\Concerns\WithPivotRelationsLoading; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Model; class User extends Model { use WithPivotRelationsLoading; public function skills(): BelongsToMany { return $this->belongsToMany(Skill::class, 'user_skill') ->using(UserSkill::class) ->withTimestamps() ->withPivot(['scale_id']) ->withPivotRelations('scale'); } }
3. Query with Eager Loading
$users = User::query()->with('skills')->get(); foreach ($users as $user) { foreach ($user->skills as $skill) { // The scale relationship is already loaded - no N+1 queries! $scale = $skill->pivot->scale; } }
Polymorphic Relationships
The package also supports MorphToMany relationships:
<?php declare(strict_types=1); namespace App\Models; use LaravelPivotRelationsEagerLoading\Concerns\WithPivotRelationsLoading; use Illuminate\Database\Eloquent\Relations\MorphToMany; use Illuminate\Database\Eloquent\Model; class Post extends Model { use WithPivotRelationsLoading; public function tags(): MorphToMany { return $this->morphToMany(Tag::class, 'taggable') ->using(Taggable::class) ->withPivotRelations('creator'); } }
For inverse polymorphic relationships, use morphedByMany():
public function posts(): MorphToMany { return $this->morphedByMany(Post::class, 'taggable') ->using(Taggable::class) ->withPivot('added_by') ->withPivotRelations('addedBy'); }
Notes:
- The package automatically detects and strips
pivot.*(oralias.*) from the relation eager-loads and loads those relations on the pivot models. This avoidsRelationNotFoundExceptionon the related model. - You can use
withPivotRelations([...])if you prefer an explicit API; both forms are supported and can be combined.
Available Classes
This package provides two classes that simply extend Laravel's native relations:
LaravelPivotRelationsEagerLoading\Relations\BelongsToMany(extendsIlluminate\Database\Eloquent\Relations\BelongsToMany)LaravelPivotRelationsEagerLoading\Relations\MorphToMany(extendsIlluminate\Database\Eloquent\Relations\MorphToMany)
Using them is optional; the trait works fine with the native Illuminate types shown in the examples above.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
abdelhamiderrahmouni/laravel-pivot-relations-eager-loading 适用场景与选型建议
abdelhamiderrahmouni/laravel-pivot-relations-eager-loading 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「pivot」 「many to many」 「laravel relationships」 「eager loading」 「Abdelhamid Errahmouni」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 abdelhamiderrahmouni/laravel-pivot-relations-eager-loading 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 abdelhamiderrahmouni/laravel-pivot-relations-eager-loading 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 abdelhamiderrahmouni/laravel-pivot-relations-eager-loading 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
belongsToMany nova representation in field.
This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.
This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.
belongsToMany nova representation in field.
Automatic auditing for Eloquent relationship changes (attach, detach, sync) using Laravel Auditing
Provides extended enterprise functionality for the Phalcon PHP framework.
统计信息
- 总下载量: 12
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 37
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-22