Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Eager Load Pivot Relations

Eager Load Pivot Relations Laravel Package

audunru/eager-load-pivot-relations

Eager load relations on pivot models in Laravel Eloquent BelongsToMany relationships. Add the EagerLoadPivotTrait to the related model to load nested pivot data such as units or quantities. Supports Laravel 8 and above.

View on GitHub
Deep Wiki
Context7

Eager Load Pivot Relations adds eager loading support for relations defined on pivot models in Laravel Eloquent's BelongsToMany relationships. A fork of ajcastro's original package, it brings compatibility with Laravel 8 and above while keeping the same simple, trait-based API.

By applying EagerLoadPivotTrait to the related model, nested pivot relations—such as a PlanItem pivot's unit—can be loaded alongside the relationship, eliminating N+1 queries.

  • Drop-in trait integration with existing belongsToMany relations
  • Eager loads relations on custom pivot models
  • Works with ->using() pivot classes and withPivot() columns
  • Supports Laravel 8 and newer
  • Fully tested with CI coverage
Frequently asked questions about Eager Load Pivot Relations
How does this package solve the N+1 query problem for BelongsToMany relationships in Laravel?
This package extends Eloquent’s eager loading to include pivot relations, allowing you to load pivot attributes and related models (e.g., pivot.unit) in a single query. For example, `Plan::with('items.pivot.unit')->get()` replaces multiple queries with one optimized query, drastically improving performance in systems like procurement or inventory.
What Laravel and PHP versions does `audunru/eager-load-pivot-relations` support?
The package supports Laravel 8 through 13 and PHP 8.1 to 8.3. It’s explicitly tested for these versions, ensuring compatibility with modern Laravel stacks. If you’re using Laravel 6.x or PHP 8.0, you’ll need to upgrade to meet the package’s requirements.
Do I need a custom pivot model to use this package?
Yes, for advanced features like eager-loading pivot relations, you’ll need to define a custom pivot model (e.g., `PlanItem`). This adds slight complexity but enables powerful use cases like `pivot.unit.someRelation`. If your pivots are simple (e.g., just IDs and timestamps), the package may not be necessary.
Can I use this package with nested eager loading (e.g., `items.pivot.unit.someRelation`)?
Absolutely. The package supports nested eager loading, allowing you to load deeply nested pivot relations in a single query. For example, `Plan::with('items.pivot.unit.someBelongsToManyRelation')->get()` will fetch all related data efficiently, though you may need to inspect queries with Laravel Debugbar for complex setups.
How do I install and set up `audunru/eager-load-pivot-relations` in my Laravel project?
Installation is straightforward: run `composer require audunru/eager-load-pivot-relations`. Then, use the `EagerLoadPivotTrait` in your Eloquent model (e.g., `Item`). Define your `BelongsToMany` relationship with `->using('CustomPivotModel')` and include pivot columns like `->withPivot('unit_id', 'qty')`. No service provider or facade is required.
Will this package work with my existing database (MySQL, PostgreSQL, SQLite)?
Yes, the package is database-agnostic and works seamlessly with MySQL, PostgreSQL, SQLite, and other databases supported by Laravel. It leverages Eloquent’s built-in query builder, so no database-specific configurations are needed.
Are there any performance trade-offs to eager loading all pivot relations?
Eager loading all pivot relations upfront can fetch more data than needed, which may impact memory usage or bandwidth. To mitigate this, use conditional loading (e.g., `when()` clauses) or only eager-load pivots that are actually used in your application logic. Always measure performance before and after implementation.
Can I use custom accessors (e.g., `as('planItem')`) for pivot relations?
Yes, the package supports custom pivot accessors, allowing you to define cleaner API methods like `pivot->planItem` instead of relying on magic strings like `pivot->unit`. This improves code readability and maintainability, especially in large applications with complex pivot structures.
What alternatives exist for optimizing BelongsToMany pivot relations in Laravel?
Alternatives include writing raw SQL joins or using Laravel’s `join()` method, though these approaches are less maintainable. Another option is to restructure your data model (e.g., using HasManyThrough or intermediate models). However, this package provides a clean, Eloquent-native solution without requiring custom query builders.
How do I test if this package is working correctly in my application?
Start by comparing query counts before and after implementation using tools like Laravel Debugbar or Blackfire. Verify that nested relations (e.g., `items.pivot.unit`) are loaded in a single query. The package includes CI/CD workflows and coverage reports, so you can also review the tests in the GitHub repository for real-world examples.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jinas/dhivatar
store-doc/laravel-storage-driver
plus-it/laravel-translation-manager
diephp/perhaps
diephp/telegraph
osmaviation/spreadsheet
microsoft/fluentui-system-icons
mmccook/php-json-canonicalization-scheme
middlewares/firewall
qubiqx/montapacking
nyoncode/laravel-package-console
pnscripts/filament-broadquill-lite
pnscripts/laravel-sentfold-lite
vleap/mx-sdk-laravel-internal
twidpay/utils
htetmyathlaing/scout-elasticsearch-driver
artisan-toolbox/front-awesome
ls/client-assistant
users-au/laravel-client
cgv/localized-resource