eloquent-best-practices

Optimize Laravel Eloquent ORM queries with eager loading and mass assignment protection.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill addresses common performance bottlenecks and pitfalls when using Laravel's Eloquent ORM, ensuring your database interactions are efficient and maintainable.

Core Features & Use Cases

  • Query Optimization: Learn techniques like eager loading and selective column retrieval to reduce database load.
  • Relationship Management: Understand best practices for defining and utilizing Eloquent relationships to avoid N+1 query problems.
  • Mass Assignment & Type Safety: Secure your models and ensure data integrity with proper fillable/guarded properties and attribute casting.
  • Use Case: Optimize a slow-loading page that displays a list of posts and their authors by implementing eager loading for the author relationship.

Quick Start

Apply the eloquent-best-practices skill to refactor the provided code snippet to use eager loading for the 'user' relationship.

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 the N+1 query problem in Laravel Eloquent?

To fix the N+1 query problem in Laravel Eloquent, implement eager loading using the `with` method to retrieve relationships in a single query, drastically reducing database load and improving application performance.

What is the best way to optimize slow Eloquent queries fetching large datasets?

The best way to optimize slow Eloquent queries is by using selective column fetching to retrieve only necessary fields and implementing query scopes, which reduces memory overhead and improves database performance.

How do I secure Laravel models against mass assignment vulnerabilities?

To secure Laravel models against mass assignment vulnerabilities, define the `fillable` or `guarded` properties, ensuring only authorized attributes can be batch updated and protecting your application from unauthorized data modification.

When do I need attribute casting in Eloquent models?

You need attribute casting in Eloquent models when storing complex data types like JSON or boolean values in database columns, ensuring proper type safety and automatic data conversion when retrieving or saving records.

Can I use query scopes to maintain Eloquent relationship logic?

Yes, you can use query scopes to encapsulate Eloquent relationship logic and common query constraints, making your database interactions more maintainable and ensuring consistent query optimization across your application.