👀 Using
with() but still getting N+1 queries?
Laravel's chaperone() fixes a sneaky Eloquent case you might be missing. 👇7cfd95c
with() but still getting N+1 queries?
Laravel's chaperone() fixes a sneaky Eloquent case you might be missing. 👇chaperone() method can help prevent **inverse N+1 query problems** when working with Eloquent relationships?
// app/Models/Post.php public function comments(): HasMany { return $this->hasMany(Comment::class) ->chaperone(); // Fixes the inverse N+1 }
$posts = Post::with('comments')->get(); foreach ($posts as $post) { foreach ($post->comments as $comment) { echo $comment->post->title; // No extra query per comment! } }
commit 7cfd95c