Skill: Craft ViewModel
"View models have one job—providing correct data to views."
The Standard
- Single Responsibility: Transform data for views. No business logic.
- Implement Arrayable: Public methods become view variables automatically.
- Optional Model: Accept
?Modelin constructor. Null for create, instance for edit. - Explicit Dependencies: Inject User, Model—whatever the view needs.
The Anti-Patterns
| ❌ Don't | ✅ Do | Why |
|---|---|---|
| View Composers | ViewModel in controller | Explicit > implicit |
| Duplicate create/edit | One ViewModel, optional model | DRY, single source of truth |
| Query in controller | Query in ViewModel method | Controller stays thin |
| Return arrays | Return ViewModel object | Type safety, testability |
Real-World Examples
See examples.md.