Eager Loading and N+1 Prevention
Load Relations Explicitly
Post::with(['author', 'comments'])->paginate();
- Use
load()/loadMissing()after fetching models when needed - Select only required columns for both base query and relations
Guard Against Lazy Loading in Dev/Test
Add to a service provider (non-production):
Model::preventLazyLoading(! app()->isProduction());
Verify
- Use a query logger or debugbar to confirm relation queries are minimized
- Add tests that assert counts or avoid unexpected query spikes