Answer the question
In order to leave comments, you need to log in
Is it possible to get a collection in which each object contains all child records in one query?
Per the documentation, standard eager loading:
$books = App\Models\Book::with('author')->get();
foreach ($books as $book) {
echo $book->author->name;
}
Answer the question
In order to leave comments, you need to log in
Author::with('books')->get(); and will get a collection of authors with their books if the hasMany link exists.
Thank you. I just looked at the result with a dump - there seem to be no books there. After the answer, I looked at the calls in the panel - everything works. I did not take into account that Laravel takes into account everything, including foreach in the blade.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question