Support the ongoing development of Laravel.io →
Article Hero Image

Limit Eager Loaded Relationships on Laravel Models

9 Sep, 2024 2 min read 241 views

Photo by Osman Rana on Unsplash

Introduction

Sometimes when you're eager loading relationships on Laravel models, you might want to limit the number of related models that are returned.

For example, on a blogging platform, you might want to load every author in the system along with three of their posts.

In older versions of Laravel, limiting eager loaded relationships was a bit of a fiddly task. I could never really find an elegant way to do it that felt right. So I usually installed Jonas Staudenmeir's (@staudenmeir) staudenmeir/eloquent-eager-limit package which adds this functionality.

However, at the beginning of this year, the core functionality behind that package was merged into the Laravel framework itself. So this means in Laravel 11, we can limit eager loaded relationships without needing to install any additional packages.

Let's take a look at an example.

Limiting Eager Loaded Relationships in Laravel

Let's say we have a blogging platform and we want to load every author in the system along with three of their posts.

We can achieve this using the limit method in the eager loading query:

use App\Models\Post;
use App\Models\Author;
use Illuminate\Contracts\Database\Eloquent\Builder;

$authors = Author::query()
    ->with([
        'posts' => fn (Builder $query): Builder => $query->limit(3),
    ])
    ->get();

As we can see in the code example above, we've defined that we want to eager load the posts relationship on the App\Models\Author model. In the eager loading query, we've used the limit method to specify that we only want to load three posts.

The Underlying SQL Queries

For anyone interested, the query above would generate two SQL queries.

The first query fetches the authors from the database:

SELECT * FROM `authors`

The second query fetches the three posts for each author (assuming we have 5 authors in the system):

SELECT *
FROM
    (
        SELECT
            *,
            row_number() OVER (PARTITION BY `posts`.`author_id`) AS `laravel_row`
        FROM `posts`
        WHERE `posts`.`author_id` IN (1, 2, 3, 4, 5)
    ) AS `laravel_table`
WHERE `laravel_row` <= 3
ORDER BY `laravel_row`

Further Reading

If you enjoyed reading this post, you might be interested in checking out my 220+ page ebook "Battle Ready Laravel" which covers similar topics in more depth.

Or, you might want to check out my other 440+ page ebook "Consuming APIs in Laravel" which teaches you how to use Laravel to consume APIs from other services.

If you're interested in getting updated each time I publish a new post, feel free to sign up for my newsletter.

Keep on building awesome stuff! ?

Last updated 1 year ago.
2
Like this article? Let the author know and give them a clap!
ash-jc-allen (Ash Allen) I'm a freelance Laravel web developer from Preston, UK. I maintain the Ash Allen Design blog and get to work on loads of cool and exciting projects ?

Other articles you might like

Article Hero Image February 5th 2026

From 400-Line Import Controllers to 20-Line Configs in Laravel

The &quot;Import Nightmares&quot; We All Know If you've built business applications with Laravel, yo...

Read article
Article Hero Image February 4th 2026

How Big Tech Generates Initial-Based Avatars — And How You Can Do the Same in Laravel

If you’ve ever created an account on platforms like Google, GitHub, Slack, or Notion, you’ve seen it...

Read article
Article Hero Image January 24th 2026

Common Laravel Mistakes I See in Production (And How to Avoid Them)

Laravel makes it incredibly easy to build applications fast. But that same ease can lead to patterns...

Read article

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2026 Laravel.io - All rights reserved.