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

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners