Black Friday: coupon FRIDAY24 for 40% off Yearly/Lifetime membership! Read more here

Eloquent Performance: 3 Most Common Mistakes

The performance of our applications is one of the top things we should care about. Inefficient Eloquent or DB queries are probably no.1 reason for bad performance. In this tutorial, I will show you the top 3 mistakes developers make when it comes to Eloquent performance, and how to fix them.


Mistake 1: Too Many DB Queries

One of the biggest and most repeating mistakes is the N+1 query issue. This is generally caused by a lot of queries to the database and not using eager loading.

Example 1: Eager Load Relationship

One of the most common example looks like this:

app/Http/Controllers/PostController.php

public function index()
{
$posts = Post::all();
 
return view('posts.index', compact('posts'));
}

And imagine you're using the Spatie Media Library package to load media files.

In the Blade file, you would use user and media relationships directly without preloading them:

resources/views/posts/index.blade.php

<ul>
@foreach($posts as $post)
<li>
{{ $post->title }} / By {{ $post->user->name }}
@foreach($post->getMedia() as $media)
{{ $media->getUrl() }}
@endforeach
</li>
@endforeach
</ul>

This produces a result similar to this, which contains a lot of database calls to get related users and media for a post:

To fix this, we can simply modify the controller to eager load the relationship like so:

  1. Change all() to get()
  2. Add the with(['user', 'media']) method to load the relationships

app/Http/Controllers/PostController.php

public function index()
{
$posts = Post::with(['user', 'media'])->get();
 
return view('posts.index', compact('posts'));
}

As a result, you will see only 3 queries being executed to load all the required data for the view:

Example 2: Counting Related Models

Another common mistake...

The full tutorial [7 mins, 1205 words] is only for Premium Members

Login Or Become a Premium Member for $129/year or $29/month
What else you will get:
  • 67 courses (1172 lessons, total 43 h 18 min)
  • 90 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent New Courses