Neversight learn skills.dev eloquent best practices 1.0.1
Skill VRIL-LABS/skill-jam/skills/best-practices/neversight-learn-skills.dev-eloquent-best-practices-1.0.1
Welcome to the skill-jam βοΈπ
npx -y skills add VRIL-LABS/skill-jam --skill neversight-learn-skills.dev-eloquent-best-practices-1.0.1Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.
2 things to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
- 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
What its author says it does
Copied from the file, not written here
Best practices for Laravel Eloquent ORM including query optimization, relationship management, and avoiding common pitfalls like N+1 queries.
SKILL.md
4.6 KB, ~1.1k tokens by cl100k_base, as published. Nobody here has run it
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
// β
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', '>', $threshold);
}
}
// Usage
$posts = Post::published()->popular()->get();
Relationship Best Practices
Define Return Types
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Post extends Model
{
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function comments(): HasMany
{
return $this->hasMany(Comment::class);
}
}
Use withCount for Counts
// β Triggers additional queries
foreach ($posts as $post) {
echo $post->comments()->count();
}
// β
Load counts efficiently
$posts = Post::withCount('comments')->get();
foreach ($posts as $post) {
echo $post->comments_count;
}
Mass Assignment Protection
class Post extends Model
{
// β
Whitelist fillable attributes
protected $fillable = ['title', 'content', 'status'];
// Or blacklist guarded attributes
protected $guarded = ['id', 'user_id'];
// β Never do this
// protected $guarded = [];
}
Use Casts for Type Safety
class Post extends Model
{
protected $casts = [
'published_at' => 'datetime',
'metadata' => 'array',
'is_featured' => 'boolean',
'views' => 'integer',
];
}
Chunking for Large Datasets
// β
Process in chunks to save memory
Post::chunk(200, function ($posts) {
foreach ($posts as $post) {
// Process each post
}
});
// β
Or use lazy collections
Post::lazy()->each(function ($post) {
// Process one at a time
});
Database-Level Operations
// β Slow - loads into memory first
$posts = Post::where('status', 'draft')->get();
foreach ($posts as $post) {
$post->update(['status' => 'archived']);
}
// β
Fast - single query
Post::where('status', 'draft')->update(['status' => 'archived']);
// β
Increment/decrement
Post::where('id', $id)->increment('views');
Use Model Events Wisely
class Post extends Model
{
protected static function booted()
{
static::creating(function ($post) {
$post->slug = Str::slug($post->title);
});
static::deleting(function ($post) {
$post->comments()->delete();
});
}
}
Common Pitfalls to Avoid
Don't Query in Loops
// β Bad
foreach ($userIds as $id) {
$user = User::find($id);
}
// β
Good
$users = User::whereIn('id', $userIds)->get();
Don't Forget Indexes
// Migration
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->index();
$table->string('slug')->unique();
$table->string('status')->index();
$table->timestamp('published_at')->nullable()->index();
// Composite index for common queries
$table->index(['status', 'published_at']);
});
Prevent Lazy Loading in Development
// In AppServiceProvider boot method
Model::preventLazyLoading(!app()->isProduction());
Checklist
- Relationships eagerly loaded where needed
- Only selecting required columns
- Using query scopes for reusability
- Mass assignment protection configured
- Appropriate casts defined
- Indexes on foreign keys and query columns
- Using database-level operations when possible
- Chunking for large datasets
- Model events used appropriately
- Lazy loading prevented in development
What ships with it: 11 files
1.8 KB alongside SKILL.md
- description_ar.txt196 B
- description_cn.txt114 B
- description_de.txt154 B
- description_en.txt142 B
- description_es.txt171 B
- description_fr.txt178 B
- description_it.txt157 B
- description_ja.txt159 B
- description_ko.txt145 B
- description_ru.txt278 B
- description_tw.txt114 B