← PHP / Laravel · Contents

PHP-089 / Intermediate

N+1 or DoS through nested Eloquent relationship loading

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

Why is this a problem?

Allowing request parameters to determine loading depth can generate numerous queries.

How to fix it

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

Further reading