eloquent-best-practices

Apply Laravel Eloquent best practices to optimize queries and model behavior.

Updated Aug 27, 2026
One-click install
npx skills add https://github.com/mkopcic/md-solutions.hr-racuni-app --skill eloquent-best-practices-mkopcic
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: eloquent-best-practices
Source: https://github.com/mkopcic/md-solutions.hr-racuni-app/tree/main/.github/skills/eloquent-best-practices
Command: npx skills add https://github.com/mkopcic/md-solutions.hr-racuni-app --skill eloquent-best-practices-mkopcic

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Eloquent best practices help developers write efficient, maintainable Laravel ORM code and avoid common pitfalls like N+1 queries and improper relationship handling.

Core Features & Use Cases

  • Query optimization: Minimizes queries via eager loading, selective columns, and efficient batch operations.
  • Relationship management: Defines clear relationships and uses withCount for performance.
  • Security & maintenance: Encourages proper mass assignment protection, casts, and event usage for robust models.

Quick Start

Refactor an existing Post model to apply eager loading, proper scopes, and safe mass assignment for improved performance.

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 Eloquent N+1 query problems by applying eager loading with the `with()` method to load relationships upfront, minimizing queries when reading related models. Selective columns and efficient batch operations further optimize data access.

What is the best way to prevent mass assignment vulnerabilities in Laravel models?

Prevent mass assignment vulnerabilities by defining `$fillable` or `$guarded` properties on your Eloquent models. This ensures only explicitly allowed fields can be updated during batch operations, securing data integrity.

How do I optimize Laravel Eloquent queries for large datasets?

Optimize Eloquent queries for large datasets by selecting only necessary columns, using eager loading to avoid N+1 issues, and implementing efficient batch operations for updating records to reduce memory consumption.

When should I use withCount for Eloquent relationship management?

Use `withCount` in Eloquent relationship management when you need to retrieve the number of related models without loading the actual records. This approach significantly improves performance by aggregating counts at the database level.

How do model events and casts improve Laravel ORM maintenance?

Model events and casts improve ORM maintenance by automatically handling data transformations and triggering actions on lifecycle changes. This ensures consistent data formatting and robust model behavior across typical app scenarios.

Does this Eloquent optimization approach apply to existing Laravel applications?

Yes, you can apply these Eloquent best practices to existing Laravel applications. The approach involves refactoring current models, such as a Post model, to implement eager loading, scopes, and safe mass assignment for better performance.