Sling Academy
Home/PHP/Eager Loading in Laravel Eloquent: Explained with Examples

Eager Loading in Laravel Eloquent: Explained with Examples

Last updated: January 18, 2024

Introduction

Laravel, known for its elegant syntax and advanced features, offers developers the ORM (Object Relational Mapping) known as Eloquent, which simplifies interaction with databases. An ORM allows developers to work with database objects and relationships using expressive syntax. One critical aspect of working with an ORM is efficiently loading related records, which brings us to the concept of eager loading. In this tutorial, we will explore what eager loading is, why it is necessary, and how it is implemented in Laravel Eloquent with examples to help you understand and apply this powerful feature in your projects.

Understanding the N+1 Query Problem

Before diving into eager loading, it is crucial to understand the N+1 query problem. Imagine you have a model called Post, which has a many-to-one relationship with a User model. If you want to display all posts along with their authors, a typical loop might look like this:

$posts = Post::all();
foreach($posts as $post) {
    echo $post->title . ' posted by ' . $post->user->name;
}

This seems simple, but it can be quite inefficient. Firstly, the Post::all() method would run one query to retrieve all posts. Then, for each post, a new query is executed to retrieve the author’s information. This means if there were 10 posts, you would end up executing 11 queries: one for all posts and 10 additional queries for each post’s author. This is the N+1 query problem.

What is Eager Loading?

Eager loading solves the N+1 query problem by retrieving related models in advance through a single query. Laravel’s Eloquent ORM allows you to specify which relationships should be loaded along with your models to prevent the N+1 query problem. Eager loading is accomplished in Laravel using the with method, which takes in the names of the relationships to load.

Code Example: Eager Loading

$posts = Post::with('user')->get();
foreach($posts as $post) {
    echo $post->title . ' posted by ' . $post->user->name;
}

In this code, we are loading all posts and their related user information in only two queries, regardless of the number of posts. This is achieved by using the with method to specify that we want the User relationship loaded along with our Post objects.

Advanced Eager Loading

Loading Multiple Relationships

With eager loading, you are not limited to a single relationship. If your model has multiple relationships, you can load them all at once using an array:

$books = Book::with(['author', 'publisher'])->get();

Nested Eager Loading

When your relationships are nested, meaning a relation of a relation, eager loading them is just as easy. You can specify the nested relationships using the ‘dot’ syntax:

$users = User::with('posts.comments')->get();

Eager Loading Constraints

Sometimes, you may want to impose certain conditions or limits on the eager loaded models. This can be done through Closure-based constraints. Here’s an example:

$users = User::with(['posts' => function ($query) {
    $query->where('status', 'published')->take(10);
}])->get();

Eager Loading at a Later Stage

There might be scenarios where you have already fetched the model and later realize you need a related model as well. Laravel offers an elegant solution to this; the load method helps in such cases:

$user = User::find(1);
// Later...
$user->load('posts');

Rounding Up

Eager loading is an indispensable feature in the Laravel Eloquent ORM that optimizes standard ORM operations and enhances performance while querying relationships between models. This tutorial aimed to provide you with practical examples to understand and effectively use eager loading in your Laravel projects. With keen attention to eager loading, you can write more efficient and scalable applications, preventing the N+1 query issue and significantly reducing the number of queries made to the database.

Next Article: How to disable/enable timestamps in Laravel Eloquent

Previous Article: Laravel Eloquent: Using whereJSONContains() to filter JSON columns

Series: Laravel & Eloquent Tutorials

PHP

You May Also Like

  • Pandas DataFrame.value_counts() method: Explained with examples
  • Constructor Property Promotion in PHP: Tutorial & Examples
  • Understanding mixed types in PHP (5 examples)
  • Union Types in PHP: A practical guide (5 examples)
  • PHP: How to implement type checking in a function (PHP 8+)
  • Symfony + Doctrine: Implementing cursor-based pagination
  • Laravel + Eloquent: How to Group Data by Multiple Columns
  • PHP: How to convert CSV data to HTML tables
  • Using ‘never’ return type in PHP (PHP 8.1+)
  • Nullable (Optional) Types in PHP: A practical guide (5 examples)
  • Explore Attributes (Annotations) in Modern PHP (5 examples)
  • An introduction to WeakMap in PHP (6 examples)
  • Type Declarations for Class Properties in PHP (5 examples)
  • Static Return Type in PHP: Explained with examples
  • PHP: Using DocBlock comments to annotate variables
  • PHP: How to ping a server/website and get the response time
  • PHP: 3 Ways to Get City/Country from IP Address
  • PHP: How to find the mode(s) of an array (4 examples)
  • PHP: Calculate standard deviation & variance of an array