20 Laravel Eloquent Performance Tips to Supercharge Your App

Laravel’s Eloquent ORM is powerful and elegant, but if used carelessly, it can silently slow your app down. Here are 20 actionable tips to make your Eloquent queries lean, fast, and efficient, all with simple examples.
1. Use Eager Loading to Fix the N+1 Problem
Avoid calling related models inside loops. Use with() to load them all at once.
// Bad: Triggers multiple queries $orders = Order::all(); foreach ($orders as $order) { echo $order->customer->name; } // Good: Only 2 queries $orders = Order::with('customer')->get();
2. Load Only the Columns You Need
Don’t use * if you only need a few columns, it saves memory and speeds up hydration.
$users = User::select('id', 'name')->get();
3. Use Chunking for Large Datasets
Instead of loading thousands of rows at once, process them in chunks.
User::chunk(500, function ($users) { foreach ($users as $user) { // Process user } });
4. Cache Frequent Queries
If a query result doesn’t change often, store it in cache.
$stats = Cache::remember('dashboard_stats', 3600, fn() => User::count());
5. Use pluck() When You Only Need One Column
Fetching a whole model when you only need one value wastes resources.
$emails = User::pluck('email');
6. Avoid Lazy Loading in Loops
When you loop through models and access relationships, you may trigger N+1 queries.
// Bad foreach (Post::all() as $post) { echo $post->comments->count(); } // Good foreach (Post::withCount('comments')->get() as $post) { echo $post->comments_count; }
7. Use Database Indexes
Add indexes to columns used in WHERE, JOIN, or ORDER BY clauses.
// migration example $table->index('email');
8. Use exists() or count() Instead of get() When Checking Data
Avoid fetching whole records if you only need to know if something exists.
if (User::where('email', $email)->exists()) { // Do something }
9. Limit Results with take() or limit()
Never fetch more than needed.
$recentUsers = User::orderBy('created_at', 'desc')->take(10)->get();
10. Use paginate() or cursorPaginate() for Big Lists
Loading everything at once isn’t scalable.
$users = User::cursorPaginate(100);
11. Use whereIn() Instead of Multiple orWhere
When filtering multiple IDs, whereIn is faster and cleaner.
$users = User::whereIn('id', [1, 2, 3])->get();
12. Use Raw Expressions Wisely
For complex queries, use raw SQL when Eloquent’s syntax adds overhead.
$users = User::selectRaw('COUNT(*) as total, status')->groupBy('status')->get();
13. Defer Relationship Loading Until Needed
If a relation is heavy, load it only when necessary.
$user = User::first(); $user->load('posts');
14. Use touches to Avoid Manual Updates on Relationships
Let Eloquent automatically update timestamps when related models change.
class Comment extends Model { protected $touches = ['post']; }
15. Disable Automatic Timestamps If Not Needed
If a model doesn’t need timestamps, disable them to skip extra queries.
public $timestamps = false;
16. Avoid Unnecessary Model Events
Model events like saving, creating, or deleting can slow down bulk operations. Use them carefully or disable when doing mass imports.
Event::fakeFor(fn() => Product::insert($bulkProducts));
17. Use updateQuietly() for Silent Updates
Prevent triggering model observers when updating frequently.
$user->updateQuietly(['last_login' => now()]);
18. Use firstWhere() for Quick Lookups
Instead of chaining where()->first(), use firstWhere() for readability and speed.
$user = User::firstWhere('email', $email);
19. Optimize Relationships with withCount and withSum
You don’t always need to fetch full relationships, just counts or sums.
$posts = Post::withCount('comments')->withSum('likes', 'value')->get();
20. Profile Your Queries
Enable Laravel’s query log or use Telescope to identify slow queries.
DB::enableQueryLog(); User::where('active', true)->get(); dd(DB::getQueryLog());
Final Thoughts
Eloquent makes database work beautiful, but beauty shouldn’t cost performance. By applying these 20 techniques, your app can handle more data, respond faster, and scale smoothly without losing Laravel’s elegance.
