eloquent-best-practices

Applies Laravel Eloquent ORM best practices for query optimization and relationship management.

Updated Nov 29, 2025
One-click install
npx skills add https://github.com/achyutkneupane/Blog-Kit --skill eloquent-best-practices-achyutkneupane
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: eloquent-best-practices
Source: https://github.com/achyutkneupane/Blog-Kit/tree/main/.ai/skills/eloquent-best-practices
Command: npx skills add https://github.com/achyutkneupane/Blog-Kit --skill eloquent-best-practices-achyutkneupane

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Laravel applications often suffer from slow database performance caused by N+1 queries, unoptimized column selection, and missing indexes. This Skill provides concrete Eloquent ORM patterns to write efficient, maintainable database code. ## Core Features & Use Cases - Query Optimization: Enforces eager loading, selective column retrieval, query scopes, and chunking for large datasets. - Relationship Management: Standardizes typed relationship definitions, withCount usage, and mass assignment protection. - Use Case: When reviewing a Laravel model or controller that loads posts with their authors, apply this Skill to replace lazy-loaded relationships with eager loading, add proper casts, and prevent N+1 query issues. ## Quick Start Review my Laravel Post model and its queries for N+1 problems and apply Eloquent best practices.

Frequently Asked Questions about eloquent-best-practices

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

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

Fix N+1 queries in Laravel by eager loading relationships with the with() method, such as Post::with('user')->get(). You can also call Model::preventLazyLoading() in your AppServiceProvider during development to detect lazy loading early.

How to optimize Eloquent queries for large datasets?

Optimize large dataset queries by using chunk() to process records in batches or lazy() to iterate one record at a time. Also select only needed columns and perform updates directly at the database level instead of loading models into memory.

What is the difference between fillable and guarded in Laravel models?

The $fillable property whitelists attributes allowed for mass assignment, while $guarded blacklists attributes that cannot be mass-assigned. Never set $guarded to an empty array, as it removes all mass assignment protection.

Does Eloquent support counting related records efficiently?

Yes, Eloquent supports efficient relationship counting via withCount('comments'), which adds a comments_count attribute using a single subquery. This avoids running a separate count query for each model in a loop.

When should I use query scopes in Laravel?

Use query scopes to encapsulate reusable query logic like published() or popular() filters directly on the model. Scopes keep controllers clean and make common query constraints chainable and consistent across the application.