laravel-performance-eager-loading

star 136

Prevent N+1 queries by eager loading; enable lazy-loading protection in non-production; choose selective fields

jpcaparas By jpcaparas schedule Updated 11/3/2025

name: laravel:performance-eager-loading description: Prevent N+1 queries by eager loading; enable lazy-loading protection in non-production; choose selective fields

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
Install via CLI
npx skills add https://github.com/jpcaparas/superpowers-laravel --skill laravel-performance-eager-loading
Repository Details
star Stars 136
call_split Forks 0
navigation Branch main
article Path SKILL.md
More from Creator