My Laravel app worked fine for months. Then the users table passed 50,000 rows, the posts page started taking four seconds to load, and a nightly export job started dying with "Allowed memory size exhausted" at 3 a.m.
I did not rewrite the app. I did not add Redis. I changed four Eloquent habits, and the same pages got fast again. Here is what actually helped, in the order that mattered most.
1. Eager loading fixed the biggest slowdown
The posts page looped over posts and read $post->user->name inside the loop. That is the classic N+1 problem: one query for the posts, then one extra query per post. With 100 posts, the page ran 101 queries.
// Before: 101 queries $posts = Post::all(); // After: 2 queries $posts = Post::with('user')->get(); One word — with('user') — cut the page load from four seconds to under half a second. If your page touches a relationship inside a loop, this is always the first thing to check. Laravel Telescope shows the query count per request, which makes the problem impossible to miss once you look.
2. exists() instead of count() for simple checks
My code checked whether an email was already registered with count() > 0. That counts every matching row before answering a yes-or-no question. exists() stops at the first match:
// Counts ALL matching rows User::where('email', $email)->count() > 0 // Stops at the first match User::where('email', $email)->exists() It is a one-word change, and on a busy table it is the difference between scanning thousands of rows and stopping at the first one.
3. Chunking fixed the memory crash
The nightly export used User::all() and tried to build 50,000 model objects in memory at once. chunk() processes 500 rows at a time and frees the rest:
User::chunk(500, function ($users) { foreach ($users as $user) { // export 500 users, then move on } }); The job has not run out of memory since. If the data can change while the job runs, chunkById() is the safer version because it pages by primary key instead of offset.
4. Let the database do the maths
A dashboard page loaded every completed order into PHP just to sum the totals. The database can return one number instead of ten thousand rows:
// Loads every order, sums in PHP Order::where('status', 'completed')->get()->sum('total'); // The database returns a single number Order::where('status', 'completed')->sum('total'); Same rule for count(), avg(), min() and max(): if the answer is one number, never fetch the rows to compute it.
What I learned
Slow Eloquent is almost never about the framework being slow. It is about asking the database for work you do not need: extra queries in a loop, whole tables in memory, counting rows you only wanted to check. Fix those four habits first, and measure before and after — Telescope will show you the improvement in one number.
I wrote up the full list of 10 tips, with the rest of the fixes (query scopes, withCount(), firstOrCreate, caching and more) here: https://rajadevsolutions.online/blog/laravel-eloquent-tips-and-tricks
What is the slowest Eloquent query you have fixed? I would love to hear what it was.


