Laravel’s Eloquent ORM is one of the most powerful tools in the PHP ecosystem. But let’s be honest, most developers stick to get()
, where()
, and find()
. Eloquent actually has many hidden features that can help you write cleaner, faster, and more maintainable code.
In this blog, you’ll discover 10 lesser-known Eloquent methods and techniques that will level up your Laravel skills.
Table of Contents
- withWhereHas() – Filter and Eager Load in One Query
- cursorPaginate() – Paginate Large Datasets Efficiently
- sole() – Ensure Only One Record Exists
- upsert() – Insert or Update Multiple Rows at Once
- lazy() – Loop Through Big Data Without Memory Issues
- findMany() – Fetch Multiple Records Easily
- whereBelongsTo() – Query Using a Model Instance
- is() – Compare Models Safely
- firstOrCreate() and updateOrCreate() – Atomic Record Handling
- tap() – Inspect Queries in the Middle of a Chain
1. withWhereHas() – Filter and Eager Load in One Query
$posts = Post::withWhereHas('comments', function ($query) {
$query->where('approved', true);
})->get();
Why it’s useful:
- Avoids multiple queries
- Reduces memory usage
- Keeps your code concise
2. cursorPaginate() – Paginate Large Datasets Efficiently
$users = User::orderBy('id')->cursorPaginate(50);
Why it’s useful:
- Great for infinite scroll or continuous loading
- Handles large datasets without slowing down
3. sole() – Ensure Only One Record Exists
$user = User::where('email', '[email protected]')->sole();
Why it’s useful:
- Eliminates silent bugs
- Ensures data uniqueness and consistency
4. upsert() – Insert or Update Multiple Rows at Once
User::upsert(
[
['email' => '[email protected]', 'name' => 'User One'],
['email' => '[email protected]', 'name' => 'User Two'],
],
['email'], // Unique column
['name'] // Columns to update
);
Why it’s useful:
- Prevents duplicates
- Minimizes database load
5. lazy() – Loop Through Big Data Without Memory Issues
User::where('active', true)->lazy()->each(function ($user) {
// Process user
});
Why it’s useful:
- Keeps memory usage low
- Perfect for batch jobs, exports, and reporting
6. findMany() – Fetch Multiple Records Easily
$users = User::findMany([1, 3, 5]);
Why it’s useful:
- Cleaner syntax
- More efficient than multiple queries
7. whereBelongsTo() – Query Using a Model Instance
Post::whereBelongsTo($user)->get();
Why it’s useful:
- More readable and expressive
- Helps when working directly with models
8. is() – Compare Models Safely
if ($post->author->is($currentUser)) {
// Perform action
}
Why it’s useful:
- Safer than manual comparisons
- Works with unsaved models too
9. firstOrCreate() and updateOrCreate() – Atomic Record Handling
$user = User::updateOrCreate(
['email' => '[email protected]'],
['name' => 'Someone New']
);
Why it’s useful:
- Avoids duplicate records
- Clean and readable
10. tap() – Inspect Queries in the Middle of a Chain
User::where('active', true)
->tap(function ($query) {
logger($query->toSql());
})
->get();
Why it’s useful:
- Great for debugging
- Keeps the method chain intact
Final Thoughts
Laravel Eloquent is packed with powerful features that go beyond the basics. By using these lesser-known methods, you can write code that is not only more efficient but also easier to maintain and scale.
Whether you're building APIs, admin panels, or full-blown applications, these tips will give your Laravel skills an extra edge.
Useful Links
- Official Eloquent Documentation
- More Laravel Tips on Dev.to
- Follow Me on GitHub for Laravel projects and tutorials