laravel:performance-eager-loading

Apply eager loading to Eloquent models to reduce N+1 queries.

Updated Mar 30, 2026
One-click install
npx skills add https://github.com/Patkik/Multi-tenant-SaaS-Catering-V2 --skill laravel-performance-eager-loading-patkik
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: laravel:performance-eager-loading
Source: https://github.com/Patkik/Multi-tenant-SaaS-Catering-V2/tree/main/.agents/skills/performance-eager-loading
Command: npx skills add https://github.com/Patkik/Multi-tenant-SaaS-Catering-V2 --skill laravel-performance-eager-loading-patkik

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Laravel's Eloquent can trigger N+1 queries when loading relations lazily. This Skill guides you to use eager loading to fetch related data in a single query, while providing safeguards to disable lazy loading in non-production environments and by selecting only required columns to reduce payloads.

Core Features & Use Cases

  • Eager load relations to minimize queries using with(['author','comments']).
  • Load missing relations after fetch with load() or loadMissing() when needed.
  • Guard against lazy loading in development by enabling Model::preventLazyLoading(! app()->isProduction()).
  • Optimize queries by selecting only the necessary fields for base and related data.
  • Verify performance improvements via query logs, debug tools, and tests.

Quick Start

Eager-load a Post with its author and comments before pagination to minimize database 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 the N+1 query problem in Laravel Eloquent?

Fix the Laravel N+1 query problem by applying eager loading with the `with()` method to fetch related models in a single database query instead of looping through lazy-loaded relations.

What is the best way to eager load multiple relations in Laravel?

Eager load multiple Eloquent relations by passing an array to the `with()` method, such as `with(['author', 'comments'])`, which retrieves the base model and its relations efficiently in fewer queries.

How can I prevent lazy loading in Laravel during local development?

Prevent lazy loading in non-production environments by calling `Model::preventLazyLoading(! app()->isProduction())`, which throws an exception if a relation is accessed without eager loading.

How do I load missing relations on an Eloquent model after it has been fetched?

Load missing relations after fetching an Eloquent model by using the `load()` or `loadMissing()` methods, which dynamically attach the related data to your already retrieved model instances.

Does eager loading in Laravel support selecting specific columns for related models?

Yes, eager loading supports selecting specific columns to reduce payload size by specifying the required fields for both base and related data within the `with()` query constraints.

How do I verify that eager loading reduces query counts in Laravel?

Verify eager loading performance improvements by checking query logs, utilizing debugging tools, and writing tests to assert that the total database query count has been minimized.