For getting every new tutorial link Please join our telegram group

Part 13 what is difference between laravel eager vs lazy loading | why we use with() method

Why we use with() method in laravel and what is difference between laravel eloquent Eager loading and Lazy loading.


Laravel eloquent lazy loading

We have a model and it takes some relationships with other models. If we get Model data after accessing its relationship then run a SQL query to get that relationship record then it means model data pass in a foreach loop and each iteration we get relationship record and it runs SQL query it called lazy loading, We have total N records than it runs total N+1 SQL queries. We also understand many queries do slow execution.

Let take an example of lazy loading.

Tables
Tables

  posts -> id, name, image
  extra_details -> id, post_id, description
Post Model

We have a Post model and it take a 'postDetail' hasOne relationship with PostDetail model.

<?php

namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class Post extends Model {
    public function postDetail() {
        return $this->hasOne(PostDetail::class);
    }
}
PostController

Here we get all posts in index method.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;

class PostController extends Controller{
    public function index(){
       $Posts = Post::all();

       foreach($posts as $post){
          echo "Post Name".$post->name;
          echo "Post description".$post->postDetail->description;
      }
    }
}

In the above example $posts variable take only posts record and when we get description data which is a column of extra_details table and it runs extra query every time for getting 'postDetail' relationship records it is lazy loading.

Laravel eloquent eager loading

For preventing lazy loading slow execution laravel provide eager loading which runs only one query and gets all model and its relationship record with. Laravel provides a with() method for loading relationship records with main query. We use one or more relationships in with method.

Let an example of with() method

PostController

Here we get all posts with its relationship 'extraDetail' in index method.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;

class PostController extends Controller{
    public function index(){
       $Posts = Post::with('postDetail')->get();

       foreach($posts as $post){
          echo "Post Name".$post->name;
          echo "Post description".$post->postDetail->description;
      }
    }
}

In above example 'postDetail' relationship records already loaded with Post model show in loop now extra query required.

Difference between eager and lazy loading

  1. The main difference between eager and lazy loading is eager loading get all data with relationship records in single query and lazy loading require N+1 queries for getting main model and relation data.

  2. Eager loading run single query whereas lazy loading run N+1 queries.

  3. Eager loading execution speed fast and take less memory compare to lazy loading.

Here we explain laravel eloquent concepts with advanced web tutorial. Also provide laravel tutorial with hindi videos.

php laravel developer anmol sharma

Anmol Sharma

I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.

Related tutorial links