I am working with a Laravel project and I am looking for a way to solve the issue of bloated models and cross referencing between them.
I had started extracting higher level methods to a repository but this doesn't solve the issue of one method needing to know about another method.
For example a task lookup method needs to lookup a slug in another table first. I don't believe I should be placing this code into either model or repository but i'd like a single method to achieve this lookup.
/Models/Slug
/Models/Task
/Repositories/SlugRepository
/Repositories/TaskRepository
I have now started experimenting with adding a gateway/service layer with a higher level methods which can access both of the underlying repositories to complete the task.
The task service would depend on the two repositories above.
/Service/Task
findBySlug()
I think this will work but I am not sure if I should now let the controllers still access the repository directly or force everything through the service/gateway layer.
Or perhaps do away with the repositories entirely and let the services access the models directly, (Laravel abstracts db access anyway).
And on top of it all I want to keep this as simple as possible!
Can anyone confirm this method as a good choice or not or perhaps suggest an alternative?
Best Answer
In the end and to keep things simple I went for a task repository and a slug service. The slug lookup needs access to another model which is why I brought it out to a service rather than repository.
I believe this setup maintained the correct separation by keeping the repositories from needing to know anything about other models and yet extracting the lookup logic to a more manageable location.