/laravel-playbook:craft-traitfrom laravel-playbook
Crafting model traits. The trait-first integration pattern for Laravel packages.
Ships in laravel-playbook. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
"A trait is a contract without the ceremony. Add it, and the model gains superpowers."
Boot Methods: boot{TraitName}() hooks into the Eloquent lifecycle. Static method. Register creating, deleting, updating listeners here. Nothing else.
Initialize Methods: initialize{TraitName}() runs on every model instance. Property setup only -- mergeCasts, $fillable additions, attribute defaults.
Relationships: Traits define Eloquent relationships that become available on the model. Use morphToMany or morphMany for polymorphic packages. Always type the return.
Query Scopes: scope{Name}() methods for filtering. Accept the query builder as the first argument, parameters after.
Configuration via Abstract Methods: Require the model to provide its own configuration. The trait defines the contract, the model fulfills it.
Optional Overrides: Provide default (usually empty) implementations for optional configuration. Override only when needed.
Accessor/Mutator Interception: Override getAttributeValue() and setAttribute() for transparent behavior. Call parent:: for non-intercepted attributes.
Trait Naming Convention: Two patterns from Taylor's first-party packages:
Searchable, Billable -- describes what the model becomes.HasApiTokens, HasFeatures -- describes what the model gains.Concerns Decomposition: When a trait grows large, split into a Concerns/ directory and compose them into one user-facing trait. Cashier's Billable composes 6 concern traits. Each concern is focused. The user adds one trait.
Utility Traits: Beyond model traits, consider Conditionable (adds when()/unless() to builders), Tappable (adds tap() for inspection), and Macroable (adds runtime extension via macro()/mixin()). These are foundational Laravel traits that any class can use.
| Don't | Do | Why |
|---|---|---|
| Require constructor injection | Use boot/initialize methods | Models don't support constructor DI |
| Side effects in boot beyond event hooks | Limit boot to event registration | Boot runs on every model load -- keep it light |
Trait methods named create(), update(), save() | Prefix with domain verbs: assignRole(), addMedia() | Collides with Eloquent methods |
| Forget the trait name prefix on boot/initialize | boot{TraitName}(), initialize{TraitName}() | Multiple traits need unique method names |
| Return untyped relationships | Always type: BelongsToMany, MorphMany | IDE autocompletion and static analysis |
| Hardcode model classes in relationships | Use config() for model resolution | Users must be able to swap models |
| One monolithic trait with 500+ lines | Split into Concerns/ and compose | Each concern focused, one user-facing trait |
See examples.md.
What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
29 skills in this plugin
/craft-apiCrafting API surfaces. Entry points, fluent builders, progressive disclosure./craft-brandingCrafting package identity. Professional impressions from the first click./craft-ciCrafting a CI pipeline. Five workflows, zero manual gates./craft-configCrafting a config file. The self-documenting contract between package and consumer./craft-deprecationCrafting deprecations. Sunset gracefully, remove confidently./craft-docsCrafting a documentation site. Progressive depth, not progressive confusion.npx claudepluginhub attac-t/the-foundry --plugin laravel-playbook