LARAVEL
Use whenLoaded() in API resources to avoid firing queries during serialization
app/Http/Resources/UserResource.php
phppublic function toArray(Request $request): array
{
return [
'id' => $this->id,
'name' => $this->name,
'posts' => PostResource::collection(
$this->whenLoaded('posts')
),
];
}Referencing `$this->posts` directly in a resource lazy-loads the relation for every model when it wasn't eager-loaded — an N+1 hidden inside serialization. `whenLoaded('posts')` includes the key only if the relation is already in memory, and omits it otherwise. The controller decides what to load via `with()`; the resource stays honest and never triggers surprise queries. Pair with `preventLazyLoading` to catch misses.
eloquentapi-resourcesperformancen+1