Purpose of eagar loading in laravel

Purpose and benefits of eagar 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.Here is sample usage:-


$books = AppBook::with('author')->get();
foreach ($books as $book) {

echo $book->author->name;

}

Leave a Reply

Your email address will not be published. Required fields are marked *