← PHP / Laravel · 目次

PHP-089 / 中級

Eloquent のネスト関連ロードによる N+1・DoS

A04:2021 Insecure Design · CWE-400 Uncontrolled Resource Consumption

なぜ問題になるのか?

リクエストパラメータでロード深度を決めると多数のクエリが発生します。

修正方法

php
$allowed = ['posts', 'profile'];
User::with(array_intersect((array) $request->input('relations'), $allowed))->get();

参考資料