In this tutorial we're going to investigate creating view objects in Laravel. On the home page you will display a list of the latest posts but on a category page you'll display a list of posts for the specific category.

Using the new Responsable interface from Laravel 5.5 we can move this view functionality into it's own class to reuse the view and parameters from different controllers.

class HttpResponse implements Responsable This will then require a toResponse() method which will be called when you return this object from a controller.

This object will be responsible for collecting the posts and returning them with the blade view.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners