eloquent-best-practices

Provide best practices for optimizing Laravel Eloquent queries and relationships.

Updated Sep 25, 2025
One-click install
npx skills add https://github.com/angelgarciasolorzano/social-hub --skill eloquent-best-practices-angelgarciasolorzano
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: eloquent-best-practices
Source: https://github.com/angelgarciasolorzano/social-hub/tree/main/.agents/skills/eloquent-best-practices
Command: npx skills add https://github.com/angelgarciasolorzano/social-hub --skill eloquent-best-practices-angelgarciasolorzano

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) and references (resource) components.

What problem does it solve?

This Skill helps Laravel developers to optimize the performance of their Eloquent ORM queries, manage relationships effectively, and avoid common pitfalls in Laravel Eloquent usage.

Core Features & Use Cases

  • Query Optimization: Offers best practices for improving query performance, such as eager loading and selecting only needed columns.
  • Relationship Management: Provides guidance on defining relationships and utilizing them efficiently.
  • Mass Assignment Protection: Explains how to secure your models from mass assignment vulnerabilities.
  • Type Safety: Details the use of casts for ensuring the correct data types.
  • Large Datasets: Recommends best practices for handling large datasets using chunking.
  • Database-Level Operations: Shows how to perform efficient database-level operations and use model events.

Quick Start

Run the skill and follow the guidelines on optimizing Eloquent queries and relationships for your Laravel application.

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?

To fix Laravel Eloquent N+1 query problems, apply eager loading using the `with` method to fetch relationships upfront, preventing excessive database queries during model looping iterations.

What is the best way to optimize Laravel Eloquent ORM queries for large datasets?

The best way to optimize Laravel Eloquent ORM queries for large datasets is using chunking, which processes records in smaller batches to prevent memory exhaustion during extensive database operations.

How does mass assignment protection work in Laravel Eloquent models?

Mass assignment protection in Laravel Eloquent models works by defining `$fillable` or `$guarded` properties, preventing unauthorized attribute changes during bulk data injection operations.

Can I perform database-level operations directly with Laravel Eloquent?

Yes, you can perform database-level operations directly with Laravel Eloquent by executing efficient query builder methods and utilizing model events to trigger actions during record lifecycle changes.

Why should I use casts in Laravel Eloquent ORM?

You should use casts in Laravel Eloquent ORM to ensure correct data types when retrieving attributes, automatically converting stored database values into native PHP types like arrays or booleans.