ettdro
ettdro

Reputation: 350

How to properly use Laravel models?

I have a web application for hockey simulation and I was wondering the use of my models recently. I don't know if I'm using my models the "good" way or should I say, the laravel way, and if there could be future repercussions that I didn't think of.

So for example, I have a lot of utility functions in my model. I have a Team model that can return the total salary of all players and also has a getInjuredPlayers() function to retrieve a list of all the injured players.

So my models declare de Eloquent relationships AND have some utility functions that are easily accessible directly in the view by doing stuff like: $team->getInjuredPlayers();.

I was wondering if its OK to do it like this or should I put my utility functions in the controller used for the desired view and only declare the relationships in the models.

Thanks!

Upvotes: 1

Views: 138

Answers (1)

nice_dev
nice_dev

Reputation: 17805

Since your utility functions interact with the database itself with no business logic except for fetching records, which is what a model is meant for, this is perfectly fine!

However, don't use model variables inside a view. To maintain the purity of MVC, fetch the details from your model in your controller and pass that data as variables to your view blade file using compact()

Upvotes: 2

Related Questions