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
Agents
audunru/eager-load-pivot-relations
claude-code
cursor
database
laravel
eloquent

Extends Eloquent's BelongsToMany to support eager-loading relations that belong to the pivot model itself, using with('relation.pivot.nestedRelation') syntax.

Install
php artisan boost:add-skill audunru/eager-load-pivot-relations

Save this content to: AGENTS.md

# eager-load-pivot-relations

Extends Eloquent's `BelongsToMany` to support eager-loading relations that belong to the pivot model itself, using `with('relation.pivot.nestedRelation')` syntax.

## Public API

No facades or service provider. Consumers use one trait:

- `EagerLoadPivotTrait` — add to any Eloquent model that has `belongsToMany` relationships. Overrides `newBelongsToMany()` to return `EagerLoadPivotBelongsToMany` instead of the standard Laravel class.

Usage pattern (from tests):

```php
// Default pivot accessor
User::with(['cars', 'cars.pivot.color'])->find($id);

// Custom pivot accessor (defined via ->as('car_user') on the relation)
Car::with(['users', 'users.car_user.color'])->find($id);
```

The pivot model itself (e.g. `CarUser`) is a standard `Illuminate\Database\Eloquent\Relations\Pivot` subclass — no special trait needed on it.

## Dependencies

- Internal: none
- External: `illuminate/database ^13.0`, `illuminate/support ^13.0`

## Testing

```
composer test
```

Fixtures: `tests/Models/` contains `User`, `Car`, `CarUser` (pivot), `Color`, `Tire`, `Brand`. Migrations are in `tests/Database/Migrations/`. Tests cover `with()`, `load()`, and `loadMissing()` for both default (`pivot`) and custom pivot accessors, plus pagination and count edge cases.

## Conventions / Gotchas

- There is **no service provider**. Nothing is auto-registered; the trait must be added manually to each model that needs the feature.
- The trait only affects `belongsToMany` — other relation types are unaffected.
- Pivot eager loads are stripped from the main builder's eager-load list before `eagerLoadRelations()` runs, so they never hit standard Eloquent's unknown-relation path. See `src/EagerLoadPivotBelongsToMany.php::getPivotEagerLoads()`.
- The pivot model must use `->using(PivotClass::class)` and the pivot class must define the nested relations for them to be loadable.

eager-load-pivot-relations

Extends Eloquent's BelongsToMany to support eager-loading relations that belong to the pivot model itself, using with('relation.pivot.nestedRelation') syntax.

Public API

No facades or service provider. Consumers use one trait:

  • EagerLoadPivotTrait — add to any Eloquent model that has belongsToMany relationships. Overrides newBelongsToMany() to return EagerLoadPivotBelongsToMany instead of the standard Laravel class.

Usage pattern (from tests):

// Default pivot accessor
User::with(['cars', 'cars.pivot.color'])->find($id);

// Custom pivot accessor (defined via ->as('car_user') on the relation)
Car::with(['users', 'users.car_user.color'])->find($id);

The pivot model itself (e.g. CarUser) is a standard Illuminate\Database\Eloquent\Relations\Pivot subclass — no special trait needed on it.

Dependencies

  • Internal: none
  • External: illuminate/database ^13.0, illuminate/support ^13.0

Testing

composer test

Fixtures: tests/Models/ contains User, Car, CarUser (pivot), Color, Tire, Brand. Migrations are in tests/Database/Migrations/. Tests cover with(), load(), and loadMissing() for both default (pivot) and custom pivot accessors, plus pagination and count edge cases.

Conventions / Gotchas

  • There is no service provider. Nothing is auto-registered; the trait must be added manually to each model that needs the feature.
  • The trait only affects belongsToMany — other relation types are unaffected.
  • Pivot eager loads are stripped from the main builder's eager-load list before eagerLoadRelations() runs, so they never hit standard Eloquent's unknown-relation path. See src/EagerLoadPivotBelongsToMany.php::getPivotEagerLoads().
  • The pivot model must use ->using(PivotClass::class) and the pivot class must define the nested relations for them to be loadable.
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.
payerurl/telegrambot-crypto-payment-instant-payout
dskripchenko/laravel-hybrid-search
riorizkyrainey/lara-ced
mehedimi/static-vel
zharp/zharp
nonsapiens/bigquery-model-sync
lgu-bayambang-ict-office/lgu-laravel-development-standards
andydefer/laravel-support
se7enxweb/flysystem
lampwick/laravel
jeslage/statamic-emojis
micahdshackelford/rmap-laravel
draftsmaninc/draftsman
square-bit/laravel-volition
himito/laravel-mailman
htinlynn/genius-helper
maco-studios/hephaestus
elmogy/larafast
xeviant/laravel-paystack
gorilladash/laravel-website-sdk