Introduction

Mainly there is two way to load the Laravel eloquent relationships Eager loading and lazy loading by default in Laravel use lazy loading.
In a lazy loading where you only get one item at a time and then retrieve related items only when needed. However, in this case N+1 query problem occurs. In the case of eager load relationship are loaded at the time of parent model load.

Description

Let’s understand it with some example if we have a two table for store the states and there cities. Here we have a relationship in the states table of hasMany so state have a many cities.

Lazy Loading

When we run a query to get all the states and through them, and for each state, we got the database query for cities. That is lazy loading i.e we are delaying the retrieval of the cities until we needed them.

Eager loading

In the eager loading we get all the states with their cities. So that when we are getting the states, we do not make separate database query to fetch the cities, it’s already loaded with states it’s called eager loading.

Eager loading helps us to solve the N+1 query problem. Let us look at a simple example of our states and cities data.

Let’s assume we have an eloquent model state.

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class State extends Model{
   public function cities(){
      return $this->hasMany(City::class);
   }
}

And a city model

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class City extends Model{
   public function state(){
      return $this->belongsTo(State::class);
   }
}

Let’s assume that we have 10 records in our state table for getting all those records we need to do run the below query

$states = State::all();

Then we have to go to all the cities associated with each state so we do something like this

foreach ($states as $state){
    echo $state->cities~->count();
}

This is what will happen with this scenario:

The query will run once for the first time to get the 10 states, then 10 more queries to get the associated cities. That makes it 11 queries in total i.e 10(N) + 1. This is called lazy loading

But, with the help of eager loading to get 10 states and there cities, we only need two query. Let’s see it on below example

$states = State::with('cities')->get();
foreach ($states as $state){
   echo $state->cities->count();
}

After using with method we all the record and it’s run only two query for getting same amount of data and it’s help to reduce the query cost.

Conclusion

All things come with some advantage and disadvantage it’s up to us to use the right things at the right time . Both Eager loading and lazy loading have their own advantages and disadvantages. You can see some of them below :

Pros of Eager Loading

You got all the data in a single query.
There is no wait for accessing related data because it’s already loaded with there parent one.
Less number of query executed with db.

Cons of Eager Loading

You might get a data that you don’t actually need.
Initially query become slow, and might bad for a lots of records.
Of course, it’s need more memory.

Support On Demand!

                                         
Laravel