← 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();

延伸阅读