Performance: Has-many-through
MEDIUM IMPACT
This affects database query performance and page load speed by controlling how related data is fetched through multiple tables.
User::with('commentsThroughPosts')->get(); // In User model: public function commentsThroughPosts() { return $this->hasManyThrough(Comment::class, Post::class); }
foreach ($users as $user) { foreach ($user->posts as $post) { foreach ($post->comments as $comment) { // process comment } } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| N+1 queries with nested loops | Low | Low | High due to delayed data | [X] Bad |
| Eager loading with has-many-through | Low | Low | Low, data ready early | [OK] Good |