I know this thread is a little old but it was recently brought up in Discord.
This example is not an idiomatic usage of the MVC pattern and while Volt and phtml could be flexible enough to perform this operation. You should not be making the template responsible for querying the DB.
Accessing data should be handled in your Controller
or Handler
layer then the results passed to Volt via the view bag.
The opinion of where to keep "business logic" differs from framework to framework but I think the 10000 ft view:
- Put data query(relation), Validation, and Transformation in the Model.
- Data retrieval should occur in the Controller
- Presentation should happen in the view with as little logic as possible.
You will thank yourself later for keeping Templates as logicless as possible. Especially when you consider how much harder it is to unit test your Model and Controller classes when they are bound to the views.