[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M6BZB0ABJPM0MAPXMDBEZ24W":3,"skill-same:01M6BZB0ABJPM0MAPXMDBEZ24W":59},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":6,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":15,"categories":16,"keywords":17,"publishedAt":34,"linkOnly":35,"markdownContent":36,"markdownBody":37,"frontMatter":38,"refName":39,"commitSha":40,"skillDirectory":41,"forksCount":13,"repositoryHtmlUrl":42,"repositoryHomepageUrl":6,"ownerHtmlUrl":43,"ownerBlogUrl":6,"ownerDescription":6,"updatedAt":44,"install":45,"resources":58},"01M6BZB0ABJPM0MAPXMDBEZ24W","eloquent-best-practices",null,"Best practices for Laravel Eloquent ORM including query optimization, relationship management, and avoiding common pitfalls like N+1 queries.","aulaleslie\u002Ftiga-saudara-ERP","aulaleslie","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F134106027?v=4","01M6BZAMW9CS4RHAM5VZXR4T2B","01M6BZAN0CDB291GCMKPBYGA2B",0,"MIT","https:\u002F\u002Fgithub.com\u002Faulaleslie\u002Ftiga-saudara-ERP\u002Fblob\u002Fmain\u002F.agents\u002Fskills\u002Feloquent-best-practices\u002FSKILL.md",[],[18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33],"best practices","eloquent best practices","eloquent","Optimization","laravel","common","orm","management","query optimization","queries","query","relationship","laravel eloquent","common pitfalls","pitfalls","for","2026-11-10 10:12:30",false,"---\nname: eloquent-best-practices\ndescription: Best practices for Laravel Eloquent ORM including query optimization, relationship management, and avoiding common pitfalls like N+1 queries.\n---\n\n# Eloquent Best Practices\n\n## Query Optimization\n\n### Always Eager Load Relationships\n\n```php\n\u002F\u002F ❌ N+1 Query Problem\n$posts = Post::all();\nforeach ($posts as $post) {\n    echo $post->user->name; \u002F\u002F N additional queries\n}\n\n\u002F\u002F ✅ Eager Loading\n$posts = Post::with('user')->get();\nforeach ($posts as $post) {\n    echo $post->user->name; \u002F\u002F No additional queries\n}\n```\n\n### Select Only Needed Columns\n\n```php\n\u002F\u002F ❌ Fetches all columns\n$users = User::all();\n\n\u002F\u002F ✅ Only needed columns\n$users = User::select(['id', 'name', 'email'])->get();\n\n\u002F\u002F ✅ With relationships\n$posts = Post::with(['user:id,name'])->select(['id', 'title', 'user_id'])->get();\n```\n\n### Use Query Scopes\n\n```php\n\u002F\u002F ✅ Define reusable query logic\nclass Post extends Model\n{\n    public function scopePublished($query)\n    {\n        return $query->where('status', 'published')\n                    ->whereNotNull('published_at');\n    }\n    \n    public function scopePopular($query, $threshold = 100)\n    {\n        return $query->where('views', '>', $threshold);\n    }\n}\n\n\u002F\u002F Usage\n$posts = Post::published()->popular()->get();\n```\n\n## Relationship Best Practices\n\n### Define Return Types\n\n```php\nuse Illuminate\\Database\\Eloquent\\Relations\\BelongsTo;\nuse Illuminate\\Database\\Eloquent\\Relations\\HasMany;\n\nclass Post extends Model\n{\n    public function user(): BelongsTo\n    {\n        return $this->belongsTo(User::class);\n    }\n    \n    public function comments(): HasMany\n    {\n        return $this->hasMany(Comment::class);\n    }\n}\n```\n\n### Use withCount for Counts\n\n```php\n\u002F\u002F ❌ Triggers additional queries\nforeach ($posts as $post) {\n    echo $post->comments()->count();\n}\n\n\u002F\u002F ✅ Load counts efficiently\n$posts = Post::withCount('comments')->get();\nforeach ($posts as $post) {\n    echo $post->comments_count;\n}\n```\n\n## Mass Assignment Protection\n\n```php\nclass Post extends Model\n{\n    \u002F\u002F ✅ Whitelist fillable attributes\n    protected $fillable = ['title', 'content', 'status'];\n    \n    \u002F\u002F Or blacklist guarded attributes\n    protected $guarded = ['id', 'user_id'];\n    \n    \u002F\u002F ❌ Never do this\n    \u002F\u002F protected $guarded = [];\n}\n```\n\n## Use Casts for Type Safety\n\n```php\nclass Post extends Model\n{\n    protected $casts = [\n        'published_at' => 'datetime',\n        'metadata' => 'array',\n        'is_featured' => 'boolean',\n        'views' => 'integer',\n    ];\n}\n```\n\n## Chunking for Large Datasets\n\n```php\n\u002F\u002F ✅ Process in chunks to save memory\nPost::chunk(200, function ($posts) {\n    foreach ($posts as $post) {\n        \u002F\u002F Process each post\n    }\n});\n\n\u002F\u002F ✅ Or use lazy collections\nPost::lazy()->each(function ($post) {\n    \u002F\u002F Process one at a time\n});\n```\n\n## Database-Level Operations\n\n```php\n\u002F\u002F ❌ Slow - loads into memory first\n$posts = Post::where('status', 'draft')->get();\nforeach ($posts as $post) {\n    $post->update(['status' => 'archived']);\n}\n\n\u002F\u002F ✅ Fast - single query\nPost::where('status', 'draft')->update(['status' => 'archived']);\n\n\u002F\u002F ✅ Increment\u002Fdecrement\nPost::where('id', $id)->increment('views');\n```\n\n## Use Model Events Wisely\n\n```php\nclass Post extends Model\n{\n    protected static function booted()\n    {\n        static::creating(function ($post) {\n            $post->slug = Str::slug($post->title);\n        });\n        \n        static::deleting(function ($post) {\n            $post->comments()->delete();\n        });\n    }\n}\n```\n\n## Common Pitfalls to Avoid\n\n### Don't Query in Loops\n\n```php\n\u002F\u002F ❌ Bad\nforeach ($userIds as $id) {\n    $user = User::find($id);\n}\n\n\u002F\u002F ✅ Good\n$users = User::whereIn('id', $userIds)->get();\n```\n\n### Don't Forget Indexes\n\n```php\n\u002F\u002F Migration\nSchema::create('posts', function (Blueprint $table) {\n    $table->id();\n    $table->foreignId('user_id')->constrained()->index();\n    $table->string('slug')->unique();\n    $table->string('status')->index();\n    $table->timestamp('published_at')->nullable()->index();\n    \n    \u002F\u002F Composite index for common queries\n    $table->index(['status', 'published_at']);\n});\n```\n\n### Prevent Lazy Loading in Development\n\n```php\n\u002F\u002F In AppServiceProvider boot method\nModel::preventLazyLoading(!app()->isProduction());\n```\n\n## Checklist\n\n- [ ] Relationships eagerly loaded where needed\n- [ ] Only selecting required columns\n- [ ] Using query scopes for reusability\n- [ ] Mass assignment protection configured\n- [ ] Appropriate casts defined\n- [ ] Indexes on foreign keys and query columns\n- [ ] Using database-level operations when possible\n- [ ] Chunking for large datasets\n- [ ] Model events used appropriately\n- [ ] Lazy loading prevented in development\n","# Eloquent Best Practices\n\n## Query Optimization\n\n### Always Eager Load Relationships\n\n```php\n\u002F\u002F ❌ N+1 Query Problem\n$posts = Post::all();\nforeach ($posts as $post) {\n    echo $post->user->name; \u002F\u002F N additional queries\n}\n\n\u002F\u002F ✅ Eager Loading\n$posts = Post::with('user')->get();\nforeach ($posts as $post) {\n    echo $post->user->name; \u002F\u002F No additional queries\n}\n```\n\n### Select Only Needed Columns\n\n```php\n\u002F\u002F ❌ Fetches all columns\n$users = User::all();\n\n\u002F\u002F ✅ Only needed columns\n$users = User::select(['id', 'name', 'email'])->get();\n\n\u002F\u002F ✅ With relationships\n$posts = Post::with(['user:id,name'])->select(['id', 'title', 'user_id'])->get();\n```\n\n### Use Query Scopes\n\n```php\n\u002F\u002F ✅ Define reusable query logic\nclass Post extends Model\n{\n    public function scopePublished($query)\n    {\n        return $query->where('status', 'published')\n                    ->whereNotNull('published_at');\n    }\n    \n    public function scopePopular($query, $threshold = 100)\n    {\n        return $query->where('views', '>', $threshold);\n    }\n}\n\n\u002F\u002F Usage\n$posts = Post::published()->popular()->get();\n```\n\n## Relationship Best Practices\n\n### Define Return Types\n\n```php\nuse Illuminate\\Database\\Eloquent\\Relations\\BelongsTo;\nuse Illuminate\\Database\\Eloquent\\Relations\\HasMany;\n\nclass Post extends Model\n{\n    public function user(): BelongsTo\n    {\n        return $this->belongsTo(User::class);\n    }\n    \n    public function comments(): HasMany\n    {\n        return $this->hasMany(Comment::class);\n    }\n}\n```\n\n### Use withCount for Counts\n\n```php\n\u002F\u002F ❌ Triggers additional queries\nforeach ($posts as $post) {\n    echo $post->comments()->count();\n}\n\n\u002F\u002F ✅ Load counts efficiently\n$posts = Post::withCount('comments')->get();\nforeach ($posts as $post) {\n    echo $post->comments_count;\n}\n```\n\n## Mass Assignment Protection\n\n```php\nclass Post extends Model\n{\n    \u002F\u002F ✅ Whitelist fillable attributes\n    protected $fillable = ['title', 'content', 'status'];\n    \n    \u002F\u002F Or blacklist guarded attributes\n    protected $guarded = ['id', 'user_id'];\n    \n    \u002F\u002F ❌ Never do this\n    \u002F\u002F protected $guarded = [];\n}\n```\n\n## Use Casts for Type Safety\n\n```php\nclass Post extends Model\n{\n    protected $casts = [\n        'published_at' => 'datetime',\n        'metadata' => 'array',\n        'is_featured' => 'boolean',\n        'views' => 'integer',\n    ];\n}\n```\n\n## Chunking for Large Datasets\n\n```php\n\u002F\u002F ✅ Process in chunks to save memory\nPost::chunk(200, function ($posts) {\n    foreach ($posts as $post) {\n        \u002F\u002F Process each post\n    }\n});\n\n\u002F\u002F ✅ Or use lazy collections\nPost::lazy()->each(function ($post) {\n    \u002F\u002F Process one at a time\n});\n```\n\n## Database-Level Operations\n\n```php\n\u002F\u002F ❌ Slow - loads into memory first\n$posts = Post::where('status', 'draft')->get();\nforeach ($posts as $post) {\n    $post->update(['status' => 'archived']);\n}\n\n\u002F\u002F ✅ Fast - single query\nPost::where('status', 'draft')->update(['status' => 'archived']);\n\n\u002F\u002F ✅ Increment\u002Fdecrement\nPost::where('id', $id)->increment('views');\n```\n\n## Use Model Events Wisely\n\n```php\nclass Post extends Model\n{\n    protected static function booted()\n    {\n        static::creating(function ($post) {\n            $post->slug = Str::slug($post->title);\n        });\n        \n        static::deleting(function ($post) {\n            $post->comments()->delete();\n        });\n    }\n}\n```\n\n## Common Pitfalls to Avoid\n\n### Don't Query in Loops\n\n```php\n\u002F\u002F ❌ Bad\nforeach ($userIds as $id) {\n    $user = User::find($id);\n}\n\n\u002F\u002F ✅ Good\n$users = User::whereIn('id', $userIds)->get();\n```\n\n### Don't Forget Indexes\n\n```php\n\u002F\u002F Migration\nSchema::create('posts', function (Blueprint $table) {\n    $table->id();\n    $table->foreignId('user_id')->constrained()->index();\n    $table->string('slug')->unique();\n    $table->string('status')->index();\n    $table->timestamp('published_at')->nullable()->index();\n    \n    \u002F\u002F Composite index for common queries\n    $table->index(['status', 'published_at']);\n});\n```\n\n### Prevent Lazy Loading in Development\n\n```php\n\u002F\u002F In AppServiceProvider boot method\nModel::preventLazyLoading(!app()->isProduction());\n```\n\n## Checklist\n\n- [ ] Relationships eagerly loaded where needed\n- [ ] Only selecting required columns\n- [ ] Using query scopes for reusability\n- [ ] Mass assignment protection configured\n- [ ] Appropriate casts defined\n- [ ] Indexes on foreign keys and query columns\n- [ ] Using database-level operations when possible\n- [ ] Chunking for large datasets\n- [ ] Model events used appropriately\n- [ ] Lazy loading prevented in development\n",{"name":5,"description":7},"main","dfa90715374d68e7cc21cb1429b99d9f054865e5",".agents\u002Fskills\u002Feloquent-best-practices","https:\u002F\u002Fgithub.com\u002Faulaleslie\u002Ftiga-saudara-ERP","https:\u002F\u002Fgithub.com\u002Faulaleslie","2026-11-11 04:12:37",{"source":8,"skillName":5,"commands":46,"prompt":56,"zipAvailable":57},[47,50,53],{"tool":48,"command":49},"npx","npx skills add aulaleslie\u002Ftiga-saudara-ERP --skill eloquent-best-practices",{"tool":51,"command":52},"bunx","bunx skills add aulaleslie\u002Ftiga-saudara-ERP --skill eloquent-best-practices",{"tool":54,"command":55},"pnpm","pnpm dlx skills add aulaleslie\u002Ftiga-saudara-ERP --skill eloquent-best-practices","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002Faulaleslie\u002Ftiga-saudara-ERP\u002Fblob\u002Fmain\u002F.agents\u002Fskills\u002Feloquent-best-practices\u002FSKILL.md",true,[],[60,70,79,88,97,106],{"skillNo":61,"name":62,"nameZh":6,"description":63,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":6,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":64,"categories":65,"keywords":66,"publishedAt":34},"01M6BZEH108XYP7CVAEZ6EKQDK","speckit-taskstoissues","speckit.taskstoissues","https:\u002F\u002Fgithub.com\u002Faulaleslie\u002Ftiga-saudara-ERP\u002Fblob\u002Fmain\u002F.github\u002Fskills\u002Fspeckit-taskstoissues\u002FSKILL.md",[],[67,68,69],"speckit taskstoissues","speckit","true",{"skillNo":71,"name":72,"nameZh":6,"description":73,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":6,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":74,"categories":75,"keywords":76,"publishedAt":34},"01M6BZEG279XMX5T2JJSCN0KPY","speckit-tasks","speckit.tasks","https:\u002F\u002Fgithub.com\u002Faulaleslie\u002Ftiga-saudara-ERP\u002Fblob\u002Fmain\u002F.github\u002Fskills\u002Fspeckit-tasks\u002FSKILL.md",[],[68,77,78,69],"speckit tasks","tasks",{"skillNo":80,"name":81,"nameZh":6,"description":82,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":6,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":83,"categories":84,"keywords":85,"publishedAt":34},"01M6BZEF4AVDCSBAWM94RVZXPK","speckit-specify","speckit.specify","https:\u002F\u002Fgithub.com\u002Faulaleslie\u002Ftiga-saudara-ERP\u002Fblob\u002Fmain\u002F.github\u002Fskills\u002Fspeckit-specify\u002FSKILL.md",[],[68,86,87,69],"speckit specify","specify",{"skillNo":89,"name":90,"nameZh":6,"description":91,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":6,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":92,"categories":93,"keywords":94,"publishedAt":34},"01M6BZEE4Y943HB0TTK3WATPC7","speckit-plan","speckit.plan","https:\u002F\u002Fgithub.com\u002Faulaleslie\u002Ftiga-saudara-ERP\u002Fblob\u002Fmain\u002F.github\u002Fskills\u002Fspeckit-plan\u002FSKILL.md",[],[95,68,96,69],"speckit plan","plan",{"skillNo":98,"name":99,"nameZh":6,"description":100,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":6,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":101,"categories":102,"keywords":103,"publishedAt":34},"01M6BZED6FJBQS98EG264A7TDF","speckit-implement","speckit.implement","https:\u002F\u002Fgithub.com\u002Faulaleslie\u002Ftiga-saudara-ERP\u002Fblob\u002Fmain\u002F.github\u002Fskills\u002Fspeckit-implement\u002FSKILL.md",[],[68,104,105,69],"speckit implement","implement",{"skillNo":107,"name":108,"nameZh":6,"description":109,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":6,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":110,"categories":111,"keywords":112,"publishedAt":34},"01M6BZEBZJEEH16CTTSQCSYAK9","speckit-git-validate","speckit.git.validate","https:\u002F\u002Fgithub.com\u002Faulaleslie\u002Ftiga-saudara-ERP\u002Fblob\u002Fmain\u002F.github\u002Fskills\u002Fspeckit-git-validate\u002FSKILL.md",[],[113,68,114,115,69],"validate","speckit git validate","git"]