Performance: One-to-many (hasMany)
MEDIUM IMPACT
This affects how many database queries are run and how much data is loaded into memory, impacting page load speed and responsiveness.
<?php $posts = App\Models\Post::with('comments')->get(); foreach ($posts as $post) { echo $post->comments->count(); }
<?php
$posts = App\Models\Post::all();
foreach ($posts as $post) {
echo $post->comments->count();
}
| Pattern | Database Queries | Memory Usage | Response Time | Verdict |
|---|---|---|---|---|
| Lazy loading (N+1 queries) | N+1 queries | Higher due to repeated loads | Slower with many related items | [X] Bad |
| Eager loading (with) | 2 queries | Lower and predictable | Faster and scalable | [OK] Good |