laravel-performance-eager-loading

Enforce explicit eager loading of relationships in Laravel to prevent N+1 queries.

Updated Jun 8, 2025
One-click install
npx skills add https://github.com/noartem/kawa --skill laravel-performance-eager-loading-noartem
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: laravel-performance-eager-loading
Source: https://github.com/noartem/kawa/tree/main/ui/.ai/skills/laravel-performance-eager-loading
Command: npx skills add https://github.com/noartem/kawa --skill laravel-performance-eager-loading-noartem

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Prevent N+1 queries in Laravel by encouraging explicit eager loading of relationships to reduce database round-trips. It also supports guarding against lazy loading in non-production environments and promotes selecting only the necessary fields to minimize data transfer.

Core Features & Use Cases

  • Eager load relationships using with() to minimize queries and improve performance in controllers, API responses, and background jobs.
  • Guard against lazy loading in development and testing with Laravel's Model::preventLazyLoading to catch accidental lazy loading before production.
  • Enforce field selection by limiting base and related queries to only required columns, enabling predictable query performance.
  • Use in real-world scenarios like listing posts with authors and comments, and dashboards that display related data.

Quick Start

Enable eager loading across your Laravel models and enable non-production lazy-loading protection to prevent N+1 queries.

Frequently Asked Questions about laravel-performance-eager-loading

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fix N+1 query problems in Laravel?

Fix N+1 query problems in Laravel by enforcing explicit eager loading of relationships. Use methods like with(), load(), and loadMissing() to preload relations, significantly reducing database round-trips in controllers, API endpoints, and background jobs.

How does Laravel preventLazyLoading guard against N+1 queries in development?

preventLazyLoading guards against N+1 queries by throwing an exception when lazy loading occurs in non-production environments. This catches accidental lazy loading during development and testing before it impacts production performance.

What is the best way to eager load specific columns in Laravel Eloquent?

The best way to eager load specific columns in Laravel Eloquent is by enforcing selective field loading. Limit base and related queries to only the required columns using with() to minimize data transfer and ensure predictable query performance.

When should I use load() vs loadMissing() for eager loading in Laravel?

Use load() to eager load relationships on an already retrieved model, and use loadMissing() to load relations only if they haven't been loaded already. Both optimize queries by preventing redundant database round-trips across API endpoints and dashboards.

Can I enforce eager loading across admin dashboards and API endpoints in Laravel?

Yes, you can enforce eager loading across admin dashboards and API endpoints in Laravel. Apply explicit eager loading using with() for typical data access patterns like listing posts with authors and comments to optimize query performance.

Why does my Laravel API have slow query performance with Eloquent relationships?

Slow Laravel API query performance with Eloquent relationships is typically caused by N+1 query problems from lazy loading. Enable non-production lazy-loading protection and use with() to eager load relationships, minimizing database round-trips.