Stitcher.io: Laravel view models
On the Sticher.io site a new tutorial has been posted introducing you to Laravel view models. This functionality allows you to remove view-only logic from other parts and isolate it for transformation.
View models are an abstraction to simplify controller and model code. View models are responsible for providing data to a view, which would otherwise come directly from the controller or the model. They allow a better separation of concerns, and provide more flexibility for the developer.In essence, view models are simple classes that take some data, and transform it into something usable for the view.
The post starts with some of the basics behind the "view model" design pattern and jumps in to an example for a blog site. In it, the code pulls in the category listing that's needed to display the page, removing the need for it to be in the controller code. It also includes the addition of custom logic to the model and the refactoring that can help move the logic into it. The tutorial also includes a section covering some of the "niceties" that can be added including passing it directly to the view
method, returning it as JSON and returning individual properties as JSON.