N+1 в Eloquent: воспроизводим на стенде, считаем запросы и закрываем регрессии тестами
The article addresses N+1 problems in Laravel Eloquent beyond the usual brief advice about with(). The author built a small bench on Laravel 13 and SQLite and reproduced the problem. It counts queries for each treatment method and checks what the lazy-loading prohibition mode does not catch. It also examines cases where eager loading does not help or itself creates load. All numbers come from that bench on artificial data.
Про N+1 в Laravel обычно пишут три строки про with() и расходятся. Я собрал небольшой стенд на Laravel 13 и SQLite, воспроизвёл проблему, посчитал запросы для каждого способа лечения и проверил, что «не ловится» режимом запрета ленивой загрузки. Отдельно разобрал места, где eager loading не помогает или сам создаёт нагрузку. Все числа в статье получены на этом стенде на искусственных данных. Читать далее