DeX Posted March 17, 2017 Share Posted March 17, 2017 Should I be able to reference any model from any other model? Currently only the controller can access any model it wants information from but once you get into the model, you can't communicate with other models. The situation I have right now is a settings page I have where it lists all the items from a table in the database and allows you to modify them or add new ones. So I have a settings model that retrieves all items from that table and the view displays them on the page. I have another model for another page that actually uses these table items and again I need to get all the items from that table in this model. Do I have the same function in both models? Do I move them to a common model that's shared? Do I just choose one and reference them from there? Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted March 18, 2017 Share Posted March 18, 2017 It seems to me that the models shouldn't communicate between each other, but you should use the controller to perform that logic. That being said, having individual models with presumably only one public method each such as one for creating a record, one for reading a record, one for updating a record, one for deleting a record (what's it spell?) is way to specific. I would say you should have one settings model which does all these tasks. You could and should also extend that model from some likely abstract model which can do tasks common to all models. Quote Link to comment Share on other sites More sharing options...
thinsoldier Posted March 18, 2017 Share Posted March 18, 2017 Laravel's eloquent models and others I can't remember the name of tend to have a method for every other model that has a defined relationship to another model. SalesRep models know they belong to an Office model. Office models know they own SalesRep models. SalesRep models know they own CustomerRelationsMessage models. CustomerRelationsMessage models know they belong to both a SalesRep model and a Customer model. Customer models know they own CustomerRelationsMessage models and OrderInvoice models and PaymentHistory models. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.