Performance: Lazy loading and N+1 prevention
HIGH IMPACT
This concept affects database query efficiency and page load speed by reducing unnecessary data fetching and minimizing server response time.
$posts = Post::with('author')->get(); foreach ($posts as $post) { echo $post->author->name; }
foreach ($posts as $post) {
echo $post->author->name;
}| Pattern | Database Queries | Server Response Time | Page Load Impact | Verdict |
|---|---|---|---|---|
| Lazy loading causing N+1 queries | N+1 queries (1 + N) | High due to many queries | Slower LCP, delays content display | [X] Bad |
| Eager loading with with() method | 2 queries (main + related) | Low due to fewer queries | Faster LCP, quicker content display | [OK] Good |