eloquent-best-practices
This skill teaches proven patterns for writing efficient Eloquent queries, from eager loading relationships to selecting only needed columns and using query scopes. Learn how to prevent common performance pitfalls like N+1 queries, implement mass assignment protection, and leverage database-level operations for faster data manipulation.
Eloquent Best Practices teaches Laravel developers how to optimize queries and manage relationships efficiently.
AI-generated summary based on this skill's SKILL.md
Decision gist · record as of 2026-04-19
Eloquent Best Practices teaches Laravel developers how to optimize queries and manage relationships efficiently. This skill teaches proven patterns for writing efficient Eloquent queries, from eager loading relationships to selecting only needed columns and using query scopes. Learn how to prevent common performance pitfalls like N+1 queries, implement mass assignment protection, and leverage database-level operations for faster data manipulation.
Use it when
- eloquent-best-practices covers multiple optimization techniques: use eager loading (`with()`) to prevent N+1 queries.
- eloquent-best-practices teaches relationship best practices including defining relationships correctly in models.
Verify before relying
Read SKILL.md below before installing (1 file). Open directory: indexed for reading, not audited.
Install
iSerter/laravel-claude-agents/eloquent-best-practices · repository language: PHP
Open directory. Skills are indexed for reading, not audited. Review a skill's body before installing it.
Frequently asked questions
AI-generated answers based on this skill's SKILL.md and metadata
How do I avoid N+1 queries in Laravel Eloquent?
eloquent-best-practices prevents N+1 queries by teaching eager loading with the `with()` method. Instead of loading relationships lazily (one query per record), load them upfront: `User::with('posts')->get()` fetches users and their posts in two queries total. Use `withCount()` for relationship counts without loading full records, and `load()` for lazy eager loading when needed. The skill covers detecting N+1 problems and restructuring queries for optimal database performance.
What are the best practices for Eloquent ORM optimization?
eloquent-best-practices covers multiple optimization techniques: use eager loading (`with()`) to prevent N+1 queries, select only needed columns with `select()`, implement query scopes for reusable filters, chunk large datasets with `chunk()` to manage memory, leverage database indexes, and use `withCount()` instead of counting relationships manually. The skill emphasizes writing queries that execute at the database level rather than in PHP, reducing data transfer and processing overhead.
How should I manage Eloquent relationships properly?
eloquent-best-practices teaches relationship best practices including defining relationships correctly in models, using eager loading to fetch related data efficiently, understanding the difference between `with()` and `load()`, and avoiding lazy loading in loops. The skill covers one-to-many, many-to-many, and polymorphic relationships, showing how to structure queries to minimize database hits and improve application performance through proper relationship management.
What security and efficiency patterns should I implement in Laravel models?
eloquent-best-practices covers mass assignment protection using `$fillable` or `$guarded` properties, type casting with `$casts` for data safety, model events for automatic data handling, and query scopes for consistent filtering. The skill teaches how to protect against common vulnerabilities while maintaining code efficiency, ensuring your models are both secure and performant through proper Laravel conventions and patterns.
What common Eloquent pitfalls should I avoid?
eloquent-best-practices identifies key pitfalls: N+1 queries from lazy loading, selecting unnecessary columns, using `count()` instead of `withCount()`, chunking without proper ordering, and neglecting database indexes. The skill teaches recognizing anti-patterns like loading full relationships when only counts are needed, and provides concrete solutions for restructuring code to follow Eloquent best practices and maintain optimal database performance.
How do I select specific columns and optimize query performance?
eloquent-best-practices teaches using `select()` to fetch only needed columns, reducing data transfer and improving speed. Combine with eager loading: `User::select('id', 'name')->with('posts:id,user_id,title')->get()`. The skill covers chunking large datasets to manage memory, using `withCount()` for aggregations, and leveraging database-level operations instead of PHP processing for faster queries and reduced server load.
SKILL.md
Rendered from the published skill. Quoted content, verbatim.
Eloquent Best Practices
Query Optimization
Always Eager Load Relationships
// ❌ N+1 Query Problem
$posts = Post::all();
foreach ($posts as $post) {
echo $post->user->name; // N additional queries
}
// ✅ Eager Loading
$posts = Post::with('user')->get();
foreach ($posts as $post) {
echo $post->user->name; // No additional queries
}
Select Only Needed Columns
// ❌ Fetches all columns
$users = User::all();
// ✅ Only needed columns
$users = User::select(['id', 'name', 'email'])->get();
// ✅ With relationships
$posts = Post::with(['user:id,name'])->select(['id', 'title', 'user_id'])->get();
Use Query Scopes
```php // ✅ Define reusable query logic class Post extends Model { public function scopePublished($query) { return $query->where('status', 'published') ->whereNotNull('published_at'); }
public function scopePopular($query, $threshold = 100)
{
return $query->where('views', '>',
(truncated - see the full file via the links below)
File tree — 1 file
skills/eloquent-best-practices/SKILL.md
Let your AI agent find skills like this
Example. Real query, live index.
You found this page by searching. An agent finds it by wishing: SkillFed indexes 56,283 agent skills by what they can do, searchable in plain language.
wish › “Learn Eloquent ORM best practices and optimization techniques”
Give your agent the search over MCP, or paste the wish link into any chat. No install? Search from any chat →
Related skills
Laravel Specialist guides you through building production-ready Laravel 10+ applications with type-safe Eloquent models, queue workers, API resources, and Livewire components. It enforces modern PHP 8.2+ patterns, eager loading, dependency injection, and >85% test coverage across feature and unit tests. Use it for architecting database schemas, implementing authentication flows, optimizing queries, and validating code against PSR-12 standards.
Laravel Best Practices is a comprehensive reference for building scalable Laravel 13 applications, covering 31 rules organized across architecture, Eloquent patterns, controllers, validation, security, performance, and API design. Use it when structuring applications, creating models and migrations, implementing form requests, or designing APIs. Includes essential patterns for service classes, repositories, form requests, and Eloquent models.
Get expert guidance on Laravel 12.x application development, from foundational concepts like routing and database migrations to advanced patterns including Eloquent relationships, API design, and authentication with Sanctum. This skill covers the full development lifecycle—models, validation, queues, dependency injection, and best practices for building scalable web applications.
PHP Expert provides in-depth instruction on PHP 8+ language features including union types, named arguments, match expressions, attributes, and enums. Learn Laravel framework patterns, Composer dependency management, and object-oriented design principles for building robust applications.
This skill guides you through a four-phase debugging methodology designed to eliminate guesswork when troubleshooting Laravel issues. By enforcing root cause investigation before any fix attempt, it helps you avoid symptom-masking patches that create new problems. The process covers log analysis, Laravel Telescope inspection, data flow tracing, pattern comparison, hypothesis testing, and test-driven implementation.
This skill provides 33 optimization rules across 9 categories for Laravel 13 applications, covering eager loading, indexing strategies, Redis caching, pagination, and transaction handling. Use it when writing Eloquent queries, diagnosing N+1 issues, configuring indexes, or debugging slow queries with Laravel Debugbar.
More skills laravel-specialist (MIT) · laravel-patterns (MIT) · laravel-api-resource-patterns (MIT)