Load data using a collection in Magento
Learn how to load Magento blog post data using a collection in a ViewModel with dependency injection.
Lesson Content
Our data fetching logic is completely out of the template and in the ViewModel, but letβs take it a step further and fetch the real blog post data from the database.
Just like other Magento classes, the Post class is a regular class and has access to everything provided by the framework, such as dependency injection. We need an instance of our post collection class, so create a constructor with public function __construct() and pass in a new private dependency for the Collection class from Macademy\Blog\Model\ResourceModel\Post\Collection.
Weβll name this variable $collection...
Join the discussion!
Comments
Want to comment on this lesson?
Enroll in this course to leave comments.