Load data using a repository in Magento
Learn how to load Magento blog post detail data using a ViewModel and PostRepository getById in templates.
Lesson Content
Weβve already written the code to fetch data out of the database within our PostRepository by creating a factory, loading a record with it using a resource model, and throwing an exception if it is not found. We donβt need to repeat this process again. Now that this boilerplate code is set up and dealing with the database for us, all we need to do is call the getById() method on the repository.
Within the constructor of our Post ViewModel, inject a new private dependency for the PostRepositoryInterface, and assign this to the $postRepository property.
Also inject a new privat...
Join the discussion!
Comments
Want to comment on this lesson?
Enroll in this course to leave comments.