Laravel Eager Loading Explained

Laravel Eager Loading Explained

In this post, we will understand what is Eager Loading and what is Lazy Loading and how they work behind the scene when we request any data from the database engine.

What is N + 1 Queries?

N + 1 issue is a long debate and web developers always have to deal with it to manage their application performance. To understand this issue, consider the following very basic example so you don’t have to scratch your head.

$projects = Project::take(10)->get();

I am using the take() method to limit the records from database table. Here is the SQL query which will be performed on the database.

SELECT * FROM 'projects' LIMIT 10

As you already know, you can pass $projects variable to your Laravel view and display data associated with Project model. Now consider, we have another model called Task, this model will be related to Project model via a hasMany relationship.

class Project extends Model
    public function tasks()
      return $this->hasMany(Task::class);

The Project model would be related to Task via a belongsTo relationship:

class Task extends Model
    public function project()
      return $this->belongsTo(Project::class);

With above relationship defined, when we pass the $projects variable to view we can list all tasks associated to each Project.

@foreach($projects as $project)
  {{ $project->name }}
  @foreach($project->tasks as $task)
    {{ $task->title }}

Now with above whole code we are dealing with N + 1 Queries issue. In the above code snippet, we are querying 10 projects from the database but as we are iterating in the view we are actually running an extra query to load related data to each project.

Every loop in the above code snippet was calling a relationship that had not been eager loaded. So each loop iteration was causing to make an extra request to load relationship data.

So N +1 issue is, we are executing one query to load 10 projects and then 10 additional queries to load tasks associated with each project. In Laravel and any other major framework, by default, we are not loading each project’s tasks until necessary which is also called Lazy Loading.

Eager means that we’re associating all the related models for a particular result set using just one query, as opposed to having to run n queries, where n is the number of items in the initial set.

Using Eager Loading

We can fix this issue by using Eager Loading. So if we know we need to load associated data, we can use Laravel’s with() method on the model to preload the data.

$projects = Project::with('tasks')->take(10)->get();

When you perform above query all tasks data will be preloaded with each project and will be available immediately. Sometimes eager loading is very useful, for example when you are requesting data from backend using Ajax, you have to use eager loading to load all related data beforehand into the response before sending it back to the frontend.

Lazy Loading or Eager Loading will always provide the same data, but Eager Loading will increase the performance of your application. Try this approach in your application and you will see a significant improvement to your application.

Now you understand the Eager Loading and hopefully, it can help you to understand the basic of lazy loaded and eager loading.

Your little help will keep this site alive and help us to produce quality content for you.