The first sentence of the Eager Loading section from the Laravel docs is:
将Eloquent 的关系作为属性访问时,关系 数据是"延迟加载"的.这意味着关系数据不是 在您第一次访问该属性之前实际加载.
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?