laravel-eloquent

Guide Laravel Eloquent ORM model definitions, relationships, and query optimization.

22|3|Updated Jan 10, 2026
One-click install
npx skills add https://github.com/fusengine/agents --skill laravel-eloquent-fusengine
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: laravel-eloquent
Source: https://github.com/fusengine/agents/tree/main/plugins/laravel-expert/skills/laravel-eloquent
Command: npx skills add https://github.com/fusengine/agents --skill laravel-eloquent-fusengine

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill streamlines database interactions within Laravel applications by providing comprehensive guidance and best practices for using the Eloquent ORM, preventing common pitfalls like N+1 query problems and ensuring efficient data handling.

Core Features & Use Cases

  • Model Definition: Understand and create Eloquent models, including mass assignment protection ($fillable, $guarded).
  • Relationships: Define and utilize all relationship types (One-to-One, One-to-Many, Many-to-Many, Polymorphic, Through).
  • Query Optimization: Implement eager loading (with(), load()) to prevent N+1 queries and use scopes for reusable query logic.
  • Data Casting: Properly cast attributes for type safety and use accessors/mutators for data transformation.
  • Use Case: When building a blog, you'll use this Skill to define the Post model, establish its belongsTo relationship with the User model (author), and ensure posts are efficiently loaded with their author data using eager loading.

Quick Start

Use the laravel-eloquent skill to define a basic Eloquent model for a 'Product' with 'name' and 'price' fields.

Frequently Asked Questions about laravel-eloquent

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?

Fix the N+1 query problem in Laravel Eloquent by implementing eager loading using the with() or load() methods to load relationships efficiently in a single query. This approach prevents executing additional database queries when accessing relationship data on multiple models.

How do I define polymorphic relationships in Laravel Eloquent?

Define polymorphic relationships in Laravel Eloquent by using morphTo, morphMany, or morphOne methods to allow a model to belong to multiple other models on a single association. This setup enables a single table to serve multiple parent models without duplicating table structures.

What is the best way to protect against mass assignment vulnerabilities in Laravel?

Protect against mass assignment vulnerabilities in Laravel by defining the $fillable property with allowed fields or the $guarded property to block specified attributes. This mechanism ensures only explicitly authorized database columns can be mass assigned during model creation or updates.

How do I use query scopes for reusable logic in Laravel ORM?

Use query scopes in Laravel ORM by defining a scope method on the model, such as scopeActive, and applying it in queries like Model::active()->get() for reusable logic. This approach encapsulates common query constraints into clean, readable methods.

How do I cast data types and use accessors in Laravel Eloquent?

Cast data types in Laravel Eloquent by defining the $casts array to automatically convert attributes to native types like boolean or array, and use accessors for custom data transformation. This ensures type safety and handles data formatting seamlessly during model retrieval and storage.