laravel-eloquent-database

Enforce Laravel Eloquent ORM best practices for models, migrations, and queries.

1|Updated Feb 28, 2026
One-click install
npx skills add https://github.com/sasabajic/laravel-best-practice-skills --skill laravel-eloquent-database
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: laravel-eloquent-database
Source: https://github.com/sasabajic/laravel-best-practice-skills/tree/main/laravel-eloquent-database
Command: npx skills add https://github.com/sasabajic/laravel-best-practice-skills --skill laravel-eloquent-database

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill provides a comprehensive guide to writing efficient, maintainable, and best-practice-compliant code for Laravel's Eloquent ORM and database interactions, preventing common pitfalls like N+1 query problems and ensuring robust data management.

Core Features & Use Cases

  • Eloquent Model Structure: Enforces a consistent and organized order for model properties, relationships, scopes, and methods.
  • Relationship Management: Guides on defining and querying relationships effectively, including eager loading and existence checks.
  • Query Optimization: Offers strategies to prevent N+1 issues, select only necessary columns, use chunking for large datasets, and leverage database-level operations.
  • Migrations & Seeding: Outlines best practices for writing clear, rollback-friendly migrations and organizing seeders with factories.
  • Use Case: When developing a feature that displays a list of users with their associated orders, this skill ensures you use with('orders') to avoid performance degradation caused by the N+1 problem.

Quick Start

Use the laravel-eloquent-database skill to refactor the provided user model to follow best practices for Eloquent model structure.

Frequently Asked Questions about laravel-eloquent-database

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, use eager loading by applying the with() method to load relationships upfront, preventing performance degradation caused by executing a separate query for each model's associated data.

What is the best way to structure Eloquent models in Laravel?

The best way to structure Eloquent models in Laravel is to follow a consistent property order, organizing relationships, scopes, accessors, mutators, and casts systematically to ensure maintainable and best-practice-compliant database interactions.

How do I optimize Laravel database queries for large datasets?

Optimize Laravel database queries for large datasets by selecting only necessary columns, using chunking to process data in batches, and leveraging database-level operations to reduce memory overhead and improve overall Eloquent query execution speed.

How do I write rollback-friendly Laravel database migrations?

Write rollback-friendly Laravel database migrations by ensuring clear up and down methods that reverse schema changes accurately, and organize seeders with factories to populate test data efficiently without breaking existing database structures.

When should I use Eloquent scopes and accessors in Laravel?

Use Eloquent scopes and accessors in Laravel when you need to encapsulate reusable query constraints or format model attributes automatically, ensuring consistent data transformation and query logic across your application's database interactions.