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 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

View models in Laravel

Frequently asked questions about Laravel View Models
How do view models differ from Laravel’s built-in view composers?
View models are dedicated classes that encapsulate view logic and data transformation, while composers are callables (closures/functions) that inject data directly into views. View models offer reusability, type safety, and testability, whereas composers are simpler but less structured. Spatie’s package replaces composers for complex logic but can coexist for legacy cases.
Can I use view models with Livewire or Inertia.js instead of Blade?
Yes, view models work seamlessly with Livewire and Inertia.js. For Livewire, pass the view model as a property to your component. With Inertia, serialize the model using `toArray()` or `toJson()` to send structured data to your frontend. The package’s data transformation logic remains agnostic to the rendering layer.
What’s the performance impact of using view models in high-traffic APIs?
The overhead is negligible for most APIs, but instantiating view models for every request in high-throughput scenarios (e.g., 10K+ RPS) could add microseconds of latency. Optimize by caching frequently used models or lazy-loading non-critical data. Benchmark with tools like Laravel Telescope to validate impact.
How do I migrate from monolithic controllers to view models incrementally?
Start by extracting view-specific logic from controllers into new view model classes. Use the `make:view-model` Artisan command to scaffold boilerplate. Replace controller return statements with view model instantiation, then update Blade templates to use the new properties. Test each route independently before full migration.
Does this package support PHP 8.1+ features like typed properties or enums?
Yes, the package fully supports PHP 8.1+ typed properties (e.g., `public User $user;`), enums, and constructor property promotion. These features enhance IDE autocompletion, type safety, and refactoring. Ensure your Laravel version (10+) and PHP version align with these requirements.
How do I test view models—unit tests vs. integration tests?
Unit test view models by mocking dependencies (e.g., repositories, APIs) and verifying transformed data. Use integration tests to render Blade templates with the model and assert output. For Livewire/Inertia, test component rendering with the serialized model. Laravel’s testing helpers (e.g., `actingAs()`) work as usual.
Will view models break if I switch from Blade to another templating engine?
View models are decoupled from Blade and focus on data transformation, so they’ll work with any templating engine (e.g., Livewire, Inertia, or even custom renderers). However, Blade-specific logic (e.g., `@include` directives) must be handled separately. The package’s core functionality remains engine-agnostic.
Can I use view models with Eloquent relationships or raw SQL queries?
Absolutely. View models can hydrate data from Eloquent models, Query Builder results, or raw SQL. Use eager loading (`with()`) to optimize N+1 queries, and leverage Laravel’s service container to bind repositories or query builders as dependencies. The package is ORM-agnostic.
What’s the best way to handle nested view models for complex UIs (e.g., dashboards)?
Compose nested view models by injecting them as dependencies in the parent model’s constructor. For example, a `DashboardViewModel` might accept a `StatsViewModel` and a `NotificationsViewModel`. Use Laravel’s service container to manage instantiation order. Blade templates can then access nested properties directly (e.g., `@foreach($dashboard->stats->items)`).
Are there alternatives to Spatie’s view models for Laravel?
Alternatives include custom view composers, Laravel’s native `View::share()` for global data, or packages like `laravel-view-models` from other vendors. However, Spatie’s package stands out for its integration with Laravel’s ecosystem (e.g., type hints, IDE support, and seamless Blade/Livewire compatibility). For simpler needs, composers may suffice, but view models scale better for complex logic.
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