---
name: "laravel-11-eloquent-relationships"
description: "Model complex relational databases with Eloquent relationships, eager loading, and scopes."
metadata:
  author: "Taylor Otwell"
  version: "1.0.0"
  runtime_compatibility:
    - "codex"
    - "claude-code"
    - "cursor"
    - "windsurf"
triggers:
  - "eloquent polymorphic relationship"
  - "eager loading with() eloquent"
  - "local query scope laravel"
permissions:
  fs_read: true
  fs_write: "workspace"
  shell_exec: false
  net_access: false
---

# Laravel 11 Eloquent ORM Advanced Relationships & Scopes

Implements Laravel Eloquent: hasMany, belongsTo, belongsToMany with pivot tables, hasManyThrough, polymorphic relationships (morphTo, morphMany), eager loading (with()), and local/global query scopes.

## When to Invoke
Invoke this skill whenever you need to:
- eloquent polymorphic relationship
- eager loading with() eloquent
- local query scope laravel

## Execution SOP & Rules
1. Define relationship method: public function comments(): MorphMany { return $this->morphMany(Comment::class, 'commentable'); }.
2. Eager load relations: $posts = Post::with(['author', 'tags'])->paginate(20);.
3. Define scope: public function scopeActive(Builder $query): void { $query->where('status', 'active'); }.
4. Prevent lazy loading in development: Model::preventLazyLoading(!app()->isProduction());.

## Verification Checklist
- Run local linting, syntax validation, and type-checks before reporting completion.
- Ensure all created or modified files adhere to workspace boundaries and avoid uncommitted secrets.
- Verify tests or build commands exit cleanly with return code 0.
