N+1 Query (ARCH7050)
هذا المحتوى غير متوفر بلغتك بعد.
Key php.orm.n-plus-one-query · Severity warning · Category orm
Accessing related data inside a loop fires a separate query each iteration, causing the N+1 problem. One database round-trip per row adds up to severe latency.
Incorrect
Section titled “Incorrect”<?php$posts = Post::all();foreach ($posts as $post) { // every iteration runs a new query echo $post->author->name;}Correct
Section titled “Correct”<?php$posts = Post::with('author')->get();foreach ($posts as $post) { echo $post->author->name; // preloaded, no extra query}Eager load the relation before the loop with with() in a single query.