What Is Lazy Eager Loading In Laravel With Example

admin_img Posted By Bajarangi soft , Posted On 10-09-2020

Eager loading is a topic in which when fetching data, we get all the needed data items together with all (or most) related data items at the same time. This is in contrast to lazy loading where you only get one item at one go and then retrieve related items only when needed.

What Is Lazy Eager Loading In Laravel With Example

Eager Loading In Laravel

When accessing Eloquent relationships as properties, the relationship data is "lazy loaded". This means the relationship data is not actually loaded until you first access the property. However, Eloquent can "eager load" relationships at the time you query the parent model. Eager loading alleviates the N + 1 query problem. To illustrate the N + 1 query problem, consider a Video model that is related to Comments

To know about Eager Loading In Laravel please visit our Bajarangisoft  site.


Lets start.
Lazy Eager Loading In Laravel
 

Sometimes you may need to eager load a relationship after the parent model has already been retrieved. For example, this may be useful if you need to dynamically decide whether to load related models:

$Video = App\Video::all();

if ($someCondition) {
   $Video->load('Name', 'publisher');
}


If you need to set additional query constraints on the eager loading query, you may pass an array keyed by the relationships you wish to load. The array values should be Closure instances which receive the query instance:

$author->load(['Video' => function ($query) {
    $query->orderBy('published_date', 'asc');
}]);

 

To load a relationship only when it has not already been loaded, use the loadMissing method:

public function format(Video $Video)
{
    $book->loadMissing('author');

    return [
        'name' => $book->name,
        'author' => $book->author->name,
    ];
}



Nested Lazy Eager Loading & morphTo

If you would like to eager load a morphTo relationship, as well as nested relationships on the various entities that may be returned by that relationship, you may use the loadMorph method.

This method accepts the name of the morphTo relationship as its first argument, and an array of model / relationship pairs as its second argument. To help illustrate this method, let's consider the following model:

<?php

use Illuminate\Database\Eloquent\Model;

class ActivityFeed extends Model
{
    /**
     * Get the parent of the activity feed record.
     */
    public function parentable()
    {
        return $this->morphTo();
    }
}
$books = App\Book::all();

if ($someCondition) {
    $books->load('author', 'publisher');
}


In this example, let's assume EventPhoto, and Post models may create ActivityFeed models. Additionally, let's assume that Event models belong to a Calendar model, Photo models are associated with Tag models, and Post models belong to an Author model.


Using these model definitions and relationships, we may retrieve ActivityFeed model instances and eager load all parentable models and their respective nested relationships:
 

$activities = ActivityFeed::with('parentable')
    ->get()
    ->loadMorph('parentable', [
        Event::class => ['calendar'],
        Photo::class => ['tags'],
        Post::class => ['author'],
    ]);

 

Related Post