PHPackages                             abdelhamiderrahmouni/laravel-pivot-relations-eager-loading - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. abdelhamiderrahmouni/laravel-pivot-relations-eager-loading

ActiveLibrary[Database &amp; ORM](/categories/database)

abdelhamiderrahmouni/laravel-pivot-relations-eager-loading
==========================================================

A Laravel package that enables eager loading of relationships on pivot tables for BelongsToMany and MorphToMany relationships.

v1.0.0(4mo ago)06[3 PRs](https://github.com/abdelhamiderrahmouni/laravel-pivot-relations-eager-loading/pulls)MITPHPPHP ^8.2CI passing

Since Dec 22Pushed 1mo agoCompare

[ Source](https://github.com/abdelhamiderrahmouni/laravel-pivot-relations-eager-loading)[ Packagist](https://packagist.org/packages/abdelhamiderrahmouni/laravel-pivot-relations-eager-loading)[ Docs](https://github.com/abdelhamiderrahmouni/laravel-pivot-relations-eager-loading)[ GitHub Sponsors](https://github.com/AbdelhamidErrahmouni)[ RSS](/packages/abdelhamiderrahmouni-laravel-pivot-relations-eager-loading/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (14)Versions (5)Used By (0)

Laravel Pivot Relations Eager Loading
=====================================

[](#laravel-pivot-relations-eager-loading)

[![Latest Version on Packagist](https://camo.githubusercontent.com/6221eebe1a3a1c054100e1b6ac5a6f7dab80260782a603324863fb29b639b9ac/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616264656c68616d696465727261686d6f756e692f6c61726176656c2d7069766f742d72656c6174696f6e732d65616765722d6c6f6164696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/abdelhamiderrahmouni/laravel-pivot-relations-eager-loading)[![GitHub Tests Action Status](https://camo.githubusercontent.com/7e69aedbf7e7c28e70a6101b62aff2cdbb3cf96d8f0e0c0931f4ba16bf5d5642/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f616264656c68616d696465727261686d6f756e692f6c61726176656c2d7069766f742d72656c6174696f6e732d65616765722d6c6f6164696e672f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/abdelhamiderrahmouni/laravel-pivot-relations-eager-loading/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/f1198c234ee56a8f31985167c7b8491168227fbfe876142f6d69992eacdc23de/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f616264656c68616d696465727261686d6f756e692f6c61726176656c2d7069766f742d72656c6174696f6e732d65616765722d6c6f6164696e672f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/abdelhamiderrahmouni/laravel-pivot-relations-eager-loading/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/1c2273fe689576a698075eb590dfe8496812e1e9ea78ec0bc519cd826da4805b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616264656c68616d696465727261686d6f756e692f6c61726176656c2d7069766f742d72656c6174696f6e732d65616765722d6c6f6164696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/abdelhamiderrahmouni/laravel-pivot-relations-eager-loading)

A Laravel package that enables eager loading of relationships on pivot tables for `BelongsToMany` and `MorphToMany` relationships.

Problem
-------

[](#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
--------

[](#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
------------

[](#installation)

```
composer require abdelhamiderrahmouni/laravel-pivot-relations-eager-loading
```

Usage
-----

[](#usage)

### 1. Create a Custom Pivot Model

[](#1-create-a-custom-pivot-model)

Make sure to define your relationships on your custom pivot model:

```
