Skip to content

Instantly share code, notes, and snippets.

@RatJantaraksa
Created October 29, 2019 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RatJantaraksa/694f3366dd04c49b844287eaeae3f37f to your computer and use it in GitHub Desktop.
Save RatJantaraksa/694f3366dd04c49b844287eaeae3f37f to your computer and use it in GitHub Desktop.
// Lazy Loading N+1 Query
$books = App\Book::all();
foreach ($books as $book) {
echo $book->author->name;
}
// select * from books
// select * from authors where id = 1
// select * from authors where id = N
// Eager Loading 2 Query
$books = App\Book::with('author')->get();
foreach ($books as $book) {
echo $book->author->name;
}
// select * from books
// select * from authors where id in (1, 2, 3, 4, 5, ...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment