İçeriğe geç

ORM N+1 sorgusu (ARCH7079)

Anahtar php.performance.orm-n-plus-one · Önem error · Kategori performance

Bir sorgu sonucu üzerinde döngü kurup her elemanda ilişkili veriye erişmek, her satır için ayrı bir sorgu çalıştırır (N+1). İlişkiyi önceden eager load edin.

<?php
$posts = Post::all();
foreach ($posts as $post) {
echo $post->author->name;
}
<?php
$posts = Post::with('author')->get();
foreach ($posts as $post) {
echo $post->author->name;
}

Döngüde erişilen ilişkileri with() ile eager load ederek tek bir ek sorguya indirin.