Most slow Laravel pages I have debugged had the same problem. The page loads a list of 50 or 100 records, and each row shows something from a related table. The code looks clean, the tests pass, and then production gets slow as the table grows. The cause is almost always the N+1 query problem.
What an N+1 query looks like
The name comes from the query count. One query loads the parent rows. Then, for each of the N parent rows, Eloquent runs another query to load a relation. You end up with N + 1 queries for what should have been two.

$posts = Post::latest()->take(50)->get();
foreach ($posts as $post) {
echo $post->author->name; // one extra query for every post
}
With 50 posts this runs 51 queries. With 500 posts it runs 501. Each query is fast on its own, but the round trips to the database add up, and the page feels slow.
The fix: eager loading
Tell Eloquent up front which relations you need. The with() method loads all the authors in one extra query, using a WHERE IN clause, and attaches them to the posts.
$posts = Post::with('author')->latest()->take(50)->get();
foreach ($posts as $post) {
echo $post->author->name; // already loaded, no query here
}

You can check the result with the query log. Before the change you see 51 SELECT statements. After it you see two.
Find them before users do
Fixing the page you already noticed is not enough. I turn on a guard in local and staging, so any lazy load throws an exception. The error shows up while I am building the feature, not after it reaches production.
// app/Providers/AppServiceProvider.php
use Illuminate\Database\Eloquent\Model;
public function boot(): void
{
Model::preventLazyLoading(! $this->app->isProduction());
}
Two more tools help me catch these:
- Laravel Debugbar shows the query count on every page in local development.
- Laravel Telescope records slow and duplicate queries in staging, so I can see them over a day of real use.
Patterns I keep coming back to
- Nested relations:
with('author.profile')loads both levels in two queries, not one per row. - Counts: use
withCount('comments')instead of loading every comment just to count them. - Only the columns you need: a
with('author:id,name')constraint keeps the result small. - Pagination: eager load after
paginate(), or on the paginated collection withload(), so the page size still limits the work.
What I would do differently
I would add the strict-mode guard on the first day of every new Laravel project. It costs one line, and it prevents the bug instead of finding it later. The rest of the fixes are small, but they only help when you know which query is slow, so measure first.