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
Install
iSerter/laravel-claude-agents/eloquent-best-practices · repository language: PHP
git clone https://github.com/iSerter/laravel-claude-agents
cp -r laravel-claude-agents/skills/eloquent-best-practices ~/.claude/skills/eloquent-best-practicesnpx skillfed install iSerter/laravel-claude-agents/eloquent-best-practicesFrequently 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)
Read as markdown · JSON record · Browse the source repository
File tree — 1 file
skills/eloquent-best-practices/SKILL.md