Answer the question
In order to leave comments, you need to log in
Best Practice when dealing with repositories in Laravel?
Hello!
I started developing a project on Laravel and I want to build the architecture right at the beginning. In order for the controllers not to be fat and not to do their own thing, it is necessary to introduce repositories.
Am I right in thinking:
Answer the question
In order to leave comments, you need to log in
Yes, you are right about the implementation.
Frankly, with AR it looks bad. In the sense that you do not use the main "advantages" (Yes, yes, in quotes, because these very advantages make development easier and faster, but do not contribute to improving the quality of the code and its support) of this pattern.
Create a basic interface for all repositories and describe these methods in it, then extend the interface for each specific repository. You can, in addition to everything, write an abstract repository class and write a universal implementation of these most common methods in it (For each type of repository, of course). But personally, I wouldn't do that.
Yes, you are right about the implementation.
If most of your data can be taken using eloquent, you should not implement repositories. I have been working on a project for two years already, in which repositories were added to the legacy (everything is implemented as you described + as D3lphi described ), I came to the conclusion that this is a redundant layer. In Laravel, they look good only when there are a lot of complex queries and the query builder is often used. But if you still decide, remember that the repositories should not give the Eloquent model out of themselves.
First, figure out what the repository is for, here they wrote a good article on Habré https://habrahabr.ru/post/316836/ To take logic out of controllers, use services - https://laravel-news.ru/blog/tutorials/design -patt...
How does all this look with AR?
Each of these repositories will have repeating methods: all, find, and others. How to be in that case? How to move such methods into a common class?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question