laravel:eloquent-relationships

Define and load Laravel Eloquent relationships with constrained eager loading.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Laravel apps often suffer from N+1 query issues and tangled relationship loading when fetching related data. This Skill guides you to define clear Eloquent relationships and apply constrained loading, counts, sums, and safe pivot syncing to optimize data access.

Core Features & Use Cases

  • Eager loading with constraints using whereHas/has and nested relations to avoid N+1.
  • Use withCount, withSum, and withMax for simple aggregates on related data.
  • Pivot operations for many-to-many relationships (sync, syncWithoutDetaching) to keep relation data consistent.
  • Apply global and local scopes to enforce recurring constraints on related queries.
  • Handle large datasets with chunking and lazy loading to reduce memory usage.

Quick Start

Define your model relationships and load related data with constraints to prevent N+1 queries.

Frequently Asked Questions about laravel:eloquent-relationships

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

FAQPage Schema
How do I fix N+1 query issues in Laravel Eloquent relationships?

To fix N+1 query issues in Laravel Eloquent, apply eager loading using the with() method to load relationships upfront. You can also use withCount() and withSum() to fetch aggregates efficiently without triggering extra queries per model.

How do I apply constrained eager loading in Laravel?

Constrained eager loading in Laravel is done by passing a closure to the with() method to filter related models. You can also use whereHas() and has() to query based on relationship existence while applying specific conditions to the loaded relations.

What is the safest way to sync many-to-many pivot tables in Laravel?

The safest way to sync many-to-many pivot tables in Laravel is using syncWithoutDetaching(). This updates pivot data consistently without removing existing relationships, while sync() replaces the entire relationship set to maintain strict data consistency.

How do I handle large datasets with Laravel Eloquent without running out of memory?

To handle large datasets with Laravel Eloquent without memory exhaustion, use chunking and lazy loading techniques. These methods process records in smaller batches, significantly reducing memory usage when retrieving and iterating through extensive relationship data.

Can I use global and local scopes to enforce recurring constraints on related Laravel queries?

Yes, you can apply global and local scopes to enforce recurring constraints on related Laravel queries. Scopes allow you to define common query restrictions that automatically apply to relationship loading, ensuring consistent data access across your application.