laravel Eloquent eager loading and lazy loading and difference between them

laravel Eloquent eager loading and lazy loading and difference between them

The term "eager loading" refers to receiving all necessary data at once. Lazy loading, on the other hand, only provides you with one item at a time and only retrieves related items when they are actually needed.


Eloquent Lazy loading in Laravel

We have a model, and it needs to be related to other models in some way. When model data is obtained after gaining access to its relationships, and then a SQL query is executed to obtain the relationship record for each iteration, this is referred to as lazy loading.

If there are a total of N records, then N+1 SQL queries are executed. We also realize that many queries execute slowly.

Take lazy loading as an example.

Suppose we have two table - user and user_meta

user -> id, name, email, phone, password

user_meta -> id user_id, short_name, address, designation


User model has a relation with UserMeta Model

<?php
 
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
 
class User extends Model {
    public function meta() {
        return $this->hasOne(UserMeta::class);
    }
}


UserMeta model has a relation with User Model

<?php
 
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
 
class UserMeta extends Model {
    public function user() {
        return $this->hasOne(User::class);
    }
}


User Controller  -  here we using index method to get all users data

<?php
 
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
 
class UserController extends Controller{
    public function index(){
       $users = User::all();
 
       foreach($users as $user){
   
          echo "Name : " .$user->name . "<br>";
  
          echo "Designation : " .$user->meta->designation . "<br>";
   
          echo "Address : " .$user->meta->address
      }
  
    }

}


In the upper example, the $users variable only stores users records. Lazy loading occurs when extra queries are done repeatedly to obtain records for the "user meta" relationship.


Eloquent Eager loading in Laravel

Laravel has eager loading, which does just one query and retrieves all records for the model and its relationships, to prevent lazy loading and delayed execution. 

With() is a method that Laravel offers to load relationship records with the primary query. We apply the procedure to one or more relationships.

Let's look at a with() method sample.

<?php
 
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
 
class UserController extends Controller{
    public function index(){

       $users = User::with('meta')->get();
 
       foreach($users as $user){
   
          echo "Name : " .$user->name . "<br>";
  
          echo "Designation : " .$user->meta->designation . "<br>";
   
          echo "Address : " .$user->meta->address
  
      }
  
    }

}


In the example above, the "meta" relationship records had previously been loaded with the User model and were visible in the loop.


Eager and lazy loading differences

1. The primary distinction between eager and lazy loading is that eager loading obtains the entirety of the core model and relationship data in a single query while lazy loading necessitates N+1 requests.

2. Lazy loading runs N+1 queries, whereas eager loading just runs one query.

3. When compared to lazy loading, eager loading has a quick execution time and uses less memory.


Hope this article will help you to know about laravel Eloquent eager loading and lazy loading.