Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel View Models Laravel Package

spatie/laravel-view-models

Move complex view-prep logic out of controllers into dedicated Laravel view model classes. Extend Spatie\ViewModels\ViewModel to transform data for views, expose computed properties, and keep templates clean and focused.

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • Decoupling business logic from controllers: Enables cleaner separation of concerns by moving view-specific data preparation into dedicated view model classes, reducing controller bloat and improving maintainability.
  • Scaling complex UI layers: Ideal for projects with intricate frontend requirements (e.g., dashboards, multi-step forms) where controllers currently handle excessive view logic.
  • Team productivity: Accelerates onboarding for backend developers unfamiliar with frontend templating (Blade) by abstracting view-specific logic into reusable classes.
  • Roadmap for API-first architectures: Supports gradual migration from monolithic controllers to a more modular, API-driven approach by encapsulating view logic in testable, reusable components.
  • Build vs. buy: Justifies internal tooling investment over custom solutions for teams already using Laravel, reducing technical debt in view-layer code.
  • Use cases:
    • E-commerce product pages with dynamic pricing/availability logic.
    • Admin panels with nested, conditional data aggregation.
    • Public-facing sites with A/B testing or personalized content flows.

When to Consider This Package

Adopt when:

  • Controllers exceed 100+ lines (or 5+ methods) due to view-specific logic.
  • The team struggles with Blade template complexity (e.g., deeply nested conditionals, repeated data transformations).
  • Test coverage for view logic is low due to tight coupling with controllers.
  • Frontend and backend teams collaborate closely, and view contracts need explicit documentation.
  • Migrating from spaghetti controllers to a more scalable architecture is a priority.

Look elsewhere if:

  • Your project uses server-side rendering (SSR) frameworks (e.g., Next.js, Nuxt) where view logic is already abstracted.
  • The team prefers view composers for simplicity and has no need for reusable view logic.
  • You’re building a headless API with no frontend layer (view models are frontend-specific).
  • The project is small-scale (e.g., CRUD apps with minimal UI complexity).

How to Pitch It (Stakeholders)

For Executives: "This package lets us clean up messy controllers and reduce frontend-backend friction by moving view-specific logic into reusable, testable classes. It’s like giving our backend team a ‘cheat sheet’ for the frontend—no more guessing what data the UI needs. For [Project X], this could cut controller refactoring time by 30% and make onboarding new devs 2x faster by separating concerns. It’s a low-risk, high-reward upgrade to our Laravel stack."

For Engineering: *"Spatie’s laravel-view-models solves the ‘controller as a dumping ground’ problem. Instead of cramming view logic into controllers, we’ll create dedicated classes (e.g., ProductDetailViewModel) that:

  • Encapsulate data transformations (e.g., formatting dates, merging API responses).
  • Improve testability by isolating view-specific logic.
  • Reduce merge conflicts in Blade templates by defining clear data contracts. Think of it as TypeScript for Blade—explicit, reusable, and maintainable. The learning curve is minimal (just a new class type), and it plays nicely with Laravel’s existing ecosystem. Let’s pilot it on [Feature Y] to prove the value."*
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport