Download the PHP package abdelhamiderrahmouni/laravel-pivot-relations-eager-loading without Composer
On this page you can find all versions of the php package abdelhamiderrahmouni/laravel-pivot-relations-eager-loading. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download abdelhamiderrahmouni/laravel-pivot-relations-eager-loading
More information about abdelhamiderrahmouni/laravel-pivot-relations-eager-loading
Files in abdelhamiderrahmouni/laravel-pivot-relations-eager-loading
Package laravel-pivot-relations-eager-loading
Short Description A Laravel package that enables eager loading of relationships on pivot tables for BelongsToMany and MorphToMany relationships.
License MIT
Homepage https://github.com/abdelhamiderrahmouni/laravel-pivot-relations-eager-loading
Informations about the package laravel-pivot-relations-eager-loading
Laravel Pivot Relations Eager Loading
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:
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
Usage
1. Create a Custom Pivot Model
Make sure to define your relationships on your custom pivot model:
2. Add the Trait to Your Models
If you prefer to always eager load a pivot relationship with your relationship definition, you can use the withPivotRelations() method:
3. Query with Eager Loading
Polymorphic Relationships
The package also supports MorphToMany relationships:
For inverse polymorphic relationships, use morphedByMany():
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
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
- Abdelhamid Errahmouni
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-pivot-relations-eager-loading with dependencies
spatie/laravel-package-tools Version ^1.16
illuminate/contracts Version ^11.0||^12.0
illuminate/database Version ^11.0||^12.0