PHPackages                             andreisizyi/eloquent-eager-limit - 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. andreisizyi/eloquent-eager-limit

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

andreisizyi/eloquent-eager-limit
================================

Laravel Eloquent eager loading with limit

023PHP

Since Oct 15Pushed 4y ago1 watchersCompare

[ Source](https://github.com/andreisizyi/eloquent-eager-limit)[ Packagist](https://packagist.org/packages/andreisizyi/eloquent-eager-limit)[ RSS](/packages/andreisizyi-eloquent-eager-limit/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

[![CI](https://github.com/staudenmeir/eloquent-eager-limit/workflows/CI/badge.svg)](https://github.com/staudenmeir/eloquent-eager-limit/workflows/CI/badge.svg)[![Code Coverage](https://camo.githubusercontent.com/3424cf6ad0ad9ed7d6d821635d9f08f45e5ecb376c783c5b8ef692012df4b9b0/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7374617564656e6d6569722f656c6f7175656e742d65616765722d6c696d69742f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/staudenmeir/eloquent-eager-limit/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/ca9aec529bb20a7b476524a19c02b490a6ead0a105bcaeb6f53c9a6b507c5437/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7374617564656e6d6569722f656c6f7175656e742d65616765722d6c696d69742f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/staudenmeir/eloquent-eager-limit/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/b8edc6fb62c2db2548a3175c0f01cb5a23d0385d5b771a45fe6c2ed38a1c9f39/68747470733a2f2f706f7365722e707567782e6f72672f7374617564656e6d6569722f656c6f7175656e742d65616765722d6c696d69742f762f737461626c65)](https://packagist.org/packages/staudenmeir/eloquent-eager-limit)[![Total Downloads](https://camo.githubusercontent.com/4ae245abf7792e5ea234a3b50455831037380a39362f61308af33332ea1ebe84/68747470733a2f2f706f7365722e707567782e6f72672f7374617564656e6d6569722f656c6f7175656e742d65616765722d6c696d69742f646f776e6c6f616473)](https://packagist.org/packages/staudenmeir/eloquent-eager-limit)[![License](https://camo.githubusercontent.com/12467cba2c0433f3327a4d4a385756eaeec9fff4543f7d843c649e86f0d63bc4/68747470733a2f2f706f7365722e707567782e6f72672f7374617564656e6d6569722f656c6f7175656e742d65616765722d6c696d69742f6c6963656e7365)](https://packagist.org/packages/staudenmeir/eloquent-eager-limit)

Introduction
------------

[](#introduction)

This Laravel Eloquent extension allows limiting the number of eager loading results per parent using [window functions](https://en.wikipedia.org/wiki/Select_(SQL)#Limiting_result_rows).

Supports Laravel 5.5.29+.

Compatibility
-------------

[](#compatibility)

- **MySQL 5.7+**
- **MySQL 5.5~5.6**: Due to a bug in MySQL, the package only works with strict mode disabled.
    In your `config/database.php` file, set `'strict' => false,` for the MySQL connection.
- **MariaDB 10.2+**: Due to a [bug](https://jira.mariadb.org/browse/MDEV-17785) in MariaDB, the package only works with strict mode disabled.
    In your `config/database.php` file, set `'strict' => false,` for the MariaDB connection.
- **PostgreSQL 9.3+**
- **SQLite 3.25+**: The limit is ignored on older versions of SQLite. This way, your application tests still work.
- **SQL Server 2008+**

Installation
------------

[](#installation)

```
composer require staudenmeir/eloquent-eager-limit:"^1.0"

```

Usage
-----

[](#usage)

Use the `HasEagerLimit` trait in both the parent and the related model and apply `limit()/take()` to your relationship:

```
class User extends Model
{
    use \Staudenmeir\EloquentEagerLimit\HasEagerLimit;

    public function posts()
    {
        return $this->hasMany('App\Post');
    }
}

class Post extends Model
{
    use \Staudenmeir\EloquentEagerLimit\HasEagerLimit;
}

$users = User::with(['posts' => function ($query) {
    $query->latest()->limit(10);
}])->get();
```

Improve the performance of `HasOne`/`HasOneThrough`/`MorphOne` relationships by applying `limit(1)`:

```
class User extends Model
{
    use \Staudenmeir\EloquentEagerLimit\HasEagerLimit;

    public function latestPost()
    {
        return $this->hasOne('App\Post')->latest()->limit(1);
    }
}

class Post extends Model
{
    use \Staudenmeir\EloquentEagerLimit\HasEagerLimit;
}

$users = User::with('latestPost')->get();
```

You can also apply `offset()/skip()` to your relationship:

```
class User extends Model
{
    use \Staudenmeir\EloquentEagerLimit\HasEagerLimit;

    public function posts()
    {
        return $this->hasMany('App\Post');
    }
}

class Post extends Model
{
    use \Staudenmeir\EloquentEagerLimit\HasEagerLimit;
}

$users = User::with(['posts' => function ($query) {
    $query->latest()->offset(5)->limit(10);
}])->get();
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) and [CODE OF CONDUCT](.github/CODE_OF_CONDUCT.md) for details.

###  Health Score

16

—

LowBetter than 4% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity28

Early-stage or recently created project

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/60555925?v=4)[andreisizyi](/maintainers/andreisizyi)[@andreisizyi](https://github.com/andreisizyi)

### Embed Badge

![Health badge](/badges/andreisizyi-eloquent-eager-limit/health.svg)

```
[![Health](https://phpackages.com/badges/andreisizyi-eloquent-eager-limit/health.svg)](https://phpackages.com/packages/andreisizyi-eloquent-eager-limit)
```

###  Alternatives

[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k117.2M118](/packages/jdorn-sql-formatter)[propel/propel1

Propel is an open-source Object-Relational Mapping (ORM) for PHP5.

8351.6M87](/packages/propel-propel1)[jfelder/oracledb

Oracle DB driver for Laravel

11518.4k](/packages/jfelder-oracledb)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
