Support the ongoing development of Laravel.io →
Eloquent IOC Architecture
Last updated 2 years ago.
0

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.

0

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.

0

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 ;)

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

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

Your logo here?

Laravel.io

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

© 2024 Laravel.io - All rights reserved.