Laravel ViewModel: Clean Controllers and Typed View Data
Raheel Shan
May 9th, 2025 · 2 min read
A Laravel ViewModel is a plain class that gathers everything one page needs (products, categories, brands, counts) so the controller passes a single object to the view. Laravel has no built-in ViewModel, but the pattern keeps controllers short, gives every Blade template one data entry point, and lets your IDE autocomplete the properties. This post shows how I have built view models for years, with typed properties and working code.
Quick answer
- Create a class in
app/ViewModelswith one typed property per piece of data the page needs. - Fill the properties in a
handle()method, using query classes or Eloquent. - In the controller, build the ViewModel and pass it to the view as
$model. - In Blade, read everything from
$modeland add a@varhint so the IDE can autocomplete.
Why I started using view models
In .NET MVC, a Razor view declares its model class at the top and the controller must pass an instance of that class. Pass the wrong one and you get an error, and the IDE autocompletes every property. Laravel gives you neither: you hand view() an array of loose keys and remember their names. I wanted that discipline in Laravel, so I made a class that represents what a page needs. That is all a view model is.
What is a ViewModel in Laravel?
A ViewModel is a class that fulfils the data requirements of one page. Say a page lists products with a category filter and a brand filter. The page needs three things, so the class has three properties.
<?php
namespace App\ViewModels;
use Illuminate\Support\Collection;
class ProductsViewModel
{
public Collection $products;
public Collection $categories;
public Collection $brands;
}
A home page for a shop is a different page, so it gets its own class, with its own name:
class HomeViewModel
{
public Collection $banners;
/** @var Section[] */
public array $sections = [];
}
class Section
{
public string $title;
public Collection $products;
}
Filling the ViewModel with data
Now give the class a method that loads the data. Here I use query classes from the Atomic Query Construction pattern, but plain Eloquent works the same way.
namespace App\ViewModels;
use App\AQC\Brand\GetAllBrands;
use App\AQC\Category\GetAllCategories;
use App\AQC\Products\GetAllProducts;
use Illuminate\Support\Collection;
class ProductsViewModel
{
public Collection $products;
public Collection $categories;
public Collection $brands;
public function handle(array $params): static
{
$this->brands = GetAllBrands::handle($params);
$this->categories = GetAllCategories::handle($params);
$this->products = GetAllProducts::handle($params);
return $this;
}
}
Note that $params is passed to every query, so a filter in the request reaches all of them.
Using the ViewModel in a controller
The controller builds the ViewModel and passes one key to the view.
namespace App\Http\Controllers;
use App\Http\Requests\GetAllProductsRequest;
use App\ViewModels\ProductsViewModel;
class ProductController extends Controller
{
public function index(GetAllProductsRequest $request)
{
$model = (new ProductsViewModel())->handle($request->validated());
return view('products.index', ['model' => $model]);
}
}
In the template, every value comes from $model:
@php /** @var \App\ViewModels\ProductsViewModel $model */ @endphp
@foreach ($model->products as $product)
<h2>{{ $product->title }}</h2>
@endforeach
Laravel ViewModel vs passing an array
Without a view model the controller loads each piece and builds an array.
public function index(GetAllProductsRequest $request)
{
$params = $request->validated();
return view('products.index', [
'brands' => GetAllBrands::handle($params),
'categories' => GetAllCategories::handle($params),
'products' => GetAllProducts::handle($params),
]);
}
For three keys that is fine. For a dashboard with twelve it is not: the controller grows, the view depends on key names nobody checks, and a typo shows up at runtime. With a view model the controller stays the same size however much the page needs, and the class is the single place that lists the page's data.
Autocomplete in Blade
When I first wrote this post, IDEs could not autocomplete Blade variables. That has improved. With a @var hint like the one above, or with typed view props, editors that understand Blade can complete $model->products and flag a missing property. I cover the setup in Laravel Blade but smarter: autocomplete and DTO discipline with ViewModels and in Fully typed Blade views and partials in Laravel.
The spatie/laravel-view-models package
Spatie publishes spatie/laravel-view-models (install with composer require spatie/laravel-view-models). Its view models extend a base class, and every public method and property is exposed to the view automatically, so the view reads $categories directly instead of $model->categories. Returning one from a controller gives JSON by default, or you call ->view('post.form') to render a template. It is a good fit if you want that automatic exposure. I prefer my own class because the single $model key makes the data source obvious in the template.
When not to use a ViewModel
- Tiny pages with one or two variables do not need a class.
- A ViewModel should gather data, not hold business rules. Put those in actions or services and call them from
handle(). - Do not share one ViewModel across unrelated pages. One page, one ViewModel.
FAQ
What is a ViewModel in Laravel? A class that holds all the data one page needs, so the controller passes one object to the view.
Does Laravel have a built-in ViewModel? No. Laravel has view composers and view data arrays, but no ViewModel class. You write your own or use the Spatie package.
What is the difference between a ViewModel and a DTO? A DTO carries data between layers. A ViewModel is a DTO shaped for one view, and it may also load that data.
Where do I put ViewModel classes? In app/ViewModels, one class per page, named after the page, for example ProductsViewModel.
ViewModel or view composer? A view composer attaches shared data to views by name. A ViewModel is passed explicitly by the controller, so it is easier to type and test.
Structure your Laravel app with me
Fat controllers, business logic in five places, views that accept any array. Bring your codebase or your plan.
In 60 minutes I review how it is laid out and we settle modules, DTOs, ViewModels and clean controllers.
Found this useful?
Support the work that goes into these posts.