samedi 11 novembre 2017

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

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?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2hrzYsu
via IFTTT

Aucun commentaire:

Enregistrer un commentaire