Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Eloquent vs Query Builder: whereHas vs join() vs Raw SQL

Premium
2:18

While Eloquent provides powerful features, Query Builder offers more direct control over SQL queries, with potentially better performance.

This lesson will compare them through practical examples.


Example Project

For our comparison, we'll use a simple database structure with:

  • 10,000 users
  • 3 posts per user (30,000 posts total)

We'll measure the performance of similar operations using both methods and analyze the results using Laravel Debugbar.


Retrieving Posts with Their Users

Approach 1: Using Eloquent Relationships

$posts = Post::with('user')
->where('title', 'like', '%a%')
->get();

When we execute this code and check the debugbar, we see:

  • 38,000 models loaded (posts + their users)
  • 77 MB of RAM used
  • 0,24 seconds execution time
  • 2 database queries

It's important to note how Eloquent handles this operation. Instead of using...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

No comments yet…