# Craft Viewmodel

> Crafting ViewModels. Data transformation for views.

- Skill: `attac-t/craft-viewmodel` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add attac-t/craft-viewmodel`
- Raw SKILL.md: https://api.skillmd.com/api/skills/attac-t/craft-viewmodel/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: attac-t (https://skillmd.com/u/attac-t)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/attac-t/craft-viewmodel

---


# Skill: Craft ViewModel

> "View models have one job—providing correct data to views."

## The Standard

1. **Single Responsibility**: Transform data for views. No business logic.
2. **Implement Arrayable**: Public methods become view variables automatically.
3. **Optional Model**: Accept `?Model` in constructor. Null for create, instance for edit.
4. **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](examples.md).

