Support the ongoing development of →
Eloquent IOC Architecture
Last updated 1 year ago.

To me it sounds like you may want a repository. I would look into this link. You can then use the IoC to inject the repository into your controllers or what ever other classes you need them in.


Thanks, that's another 2 days of my life I'll never get back while I get my head around repositories. However, even I can see the power of this pattern, so thanks for your suggestion.

Just a quick question - for simple tasks, injecting a model directly into the controller function like so:

public function index(Model $model)
  $datasubset = $model->getDataSubsetFunction(); (defined as public function on the model)

works, but there's going to be a reason why it's not a good idea. Can you explain why not? Thanks for your guidance.


You're welcome!

Personally, I normally don't do that for testability. On top of that, if it would venture outside of adding/using query scopes, I wouldn't do it. It all revolves around the original issue of trying to keep things as dry as possible.

Additionally, I tend to only put functions in the model that are directly related to the data in the model. Leaving any real querying for repositories. For example if I had a Location model, an example function would be isOpen() to see if there was data in the object to see if it's open. Or getLatLong() (provided I would keep these separate in the database)

Certainly nothing wrong with in ejecting the model though :) It's all preference really ;)


Sign in to participate in this thread!


Your banner here too?


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 - All rights reserved.