Laravel: What is the purpose of the `loadMissing` function?

laravel wherehas
laravel hasonethrough
with in laravel
laravel orwherehas
wherepivot laravel
laravel belongsto through
laravel count relationship
laravel create many

The first sentence of the Eager Loading section from the Laravel docs is:

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.

In the last paragraph of this section it is stated:

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

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

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

But I don't see the purpose of $book->loadMissing('author'). Is it doing anything here?

What would be the difference if I just remove this line? According to the first sentence, the author in $book->author->name would be lazy-loaded anyway, right?


Laravel: What is the purpose of the `loadMissing` function , the loadMissing method: public function format(Book $book) { $book->​loadMissing('author'); return [ 'name' => $book->name, 'author'  The PHP Framework for Web Artisans. Laravel is a web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.


I believe the accepted answer is missing one important fact that may mislead some: you cannot run loadMissing($relation) on a collection.

This is important because most use cases of lazy eager loading relationships are when you already have a collection and you don't want to commit the n+1 sin - i.e. unnecessarily hit the DB multiple times in a loop.

So while you can use load($relation) on a collection, if you only want to do it if the relationships haven't already been loaded before, you're out of luck.

Eloquent: Relationships - Laravel, Laravel: What is the purpose of the `loadMissing` function? The first sentence of the Eager Loading section from the Laravel docs is:. Since all types of Eloquent relationships are defined via methods, you may call those methods to obtain an instance of the relationship without actually executing the relationship queries. In addition, all types of Eloquent relationships also serve as query builders , allowing you to continue to chain constraints onto the relationship query


Lets say you have multiple relationships.

book belongs to an author and book belongs to a publisher.

so first you might load it with one relationship.

$books->load('author');

and later on certain condition you want to load another relationship into it.

$book->loadMissing('publisher');

But I don't see the purpose of $book->loadMissing('author');. Is it doing anything here? What would be the difference if I just remove this line? According to the first sentence, the author in $book->author->name would be lazy-loaded anyway, right?

Suppose say

public function format(Book $book)
{
    //book will not have the author relationship yet  

    return [
        'name' => $book->name, //book will not have the author relationship loaded yet  
        'author' => $book->author->name //book will now have the author relationship 
    ];
}

Difference between above and below code is when will the relationship be loaded and how much control you have over the property.

public function format(Book $book)
{
    $book->loadMissing('author'); // book will now have the author relationship

    return [
        'name' => $book->name, // book have the author relationship loaded
        'author' => $book->author->name // book have the author relationship loaded
    ];
}

Laravel: What is the purpose of the `loadMissing , Laravel Version: 5.5.33 PHP Version: 7.2 Description: Hi ! I'd like to demonstrate an I took a look at the loadMissing method. As I see it, it's not  Laravel’s database seeds classes allow you to create fake data rapidly and store in the database. Believe me, it’s much better than entering data manually when you are building or testing an application.


Both answers here have covered pretty well what the technical difference is, so I'd refer you to them first. But the "why" isn't very evident.

Something I find myself preaching a lot lately is that Eloquent is really good at giving you enough rope to hang yourself with. By abstracting the developer so far away from the actual SQL queries being produced, especially with dynamic properties, it's easy to forget when your database hits are hurting your performance more than they need to.

Here's the thing. One query using an IN() statement on 1000 values takes about the same execution time as one query running on one value. SQL is really good at what it does- the performance hit usually comes with opening and closing the DB connection. It's a bit like going grocery shopping by way of making one trip to the market for each item, as opposed to getting it all done at once. Eager-loads use the IN statements.

Lazy-loading is good for instances where you're handling too much data for your server's RAM to cope with, and in my opinion, not good for much else. It handles only one entry at any given moment. But it's reconnecting each time. I can't tell you the number of times I've seen Transformer classes, which should be responsible only for reformatting data as opposed to retrieving it, leveraging those dynamic properties and not realizing that the data wasn't already there. I've seen improvements as dramatic reducing execution time from 30 minutes to 30 seconds just by adding a single line of eager-loading prior to the Transformer being called.

(By the way, batching might be considered the happy-medium, and Eloquent's chunk() method offers that too.)

To answer your question a little more directly; if you're dealing with an instance where it's a one-to-one relationship, and it's going to be used in only one place, then functionally there is no difference between load, loadMissing, or a lazy-loading dynamic property. But if you have a many-to-many, it may be worthwhile to gather up all that data all at once. One book can have many co-authors. One author can write many books. And if you're about to loop through large sets of either, go ahead and make the most of your trip to the market before you start cooking.

[5.5] "loadMissing" reload relation when loading nested relations , For us to define this relationship, we have to place a phone method on the User model. of this attribute to better reflect its purpose within our application. not already been loaded, you should use the loadMissing method: By default, Laravel will use the fully qualified class name to store the type of the related model. For instance, given the example above where a Comment may belong to a Post or a Video , the default commentable_type would be either App\Post or App\Video , respectively.


its mean do not repeat the query to be clear about it if you use : load() 2 times the query will repeat even if the relationships exists

while : loadMissing() is check if the relationship has loaded . it will not repeat the query . beacuse it has already loaded before by [ load() or with() ] = egear load

    DB::enableQueryLog();

    $user = User::find(1);

    // see the query 

    $user->load('posts');   
    $user->load('posts');  
    $user->loadMissing('posts'); // put it on top to see the difference 

    dd(DB::getQueryLog());

that's what i think its purpose

Laravel (5.7) Eloquent Relationships, To load a relationship only when it has not already been loaded, use the loadMissing() method (available only since Laravel 5.5): $contacts = Contact::all​(); if  The Laravel Schema facade provides database agnostic support for creating and manipulating tables across all of Laravel's supported database systems. Generating Migrations To create a migration, use the make:migration Artisan command :


Laravel: Up & Running: A Framework for Building Modern PHP Apps, Win $1500 in the first Laravel Blog Writing Contest Updates include a new Redis contract, Eloquent Collection::loadMissing() method, and new validation rules. It aims to allow non-technical users to quickly create any kind of website and  The Eloquent collection object extends the Laravel base collection, so it naturally inherits dozens of methods used to fluently work with the underlying array of Eloquent models. All collections also serve as iterators, allowing you to loop over them as if they were simple PHP arrays:


Eager load relationship if exists, By default, Laravel will use the fully qualified class name to store the type of the related model. For instance, given the example above where a Comment may belong to a Post or a Video , the default commentable_type would be either App\Post or App\Video , respectively.


Laravel News, In the above code, the model class Books is extended from Illuminate\Database\Eloquent\Model class of Laravel. You will be noticing that there is no database CRUD operation related functions in this model. You could use several prebuilt Eloquent functions for this purpose. Working with Eloquent ORM