Query Inside Loop (ARCH7064)
Ce contenu n’est pas encore disponible dans votre langue.
Key php.orm.query-inside-loop · Severity warning · Category orm
Issuing any database read query (find, aggregate, repository call) inside a loop body produces one round-trip per item. This is a broader performance trap than relation N+1.
Incorrect
Section titled “Incorrect”<?phpforeach ($ids as $id) { $user = User::find($id); // query each iteration process($user);}Correct
Section titled “Correct”<?php$users = User::whereIn('id', $ids)->get(); // single queryforeach ($users as $user) { process($user);}Move the in-loop query into a single batch query (whereIn) outside the loop.