V
V
Vlad2017-10-04 16:08:05
Laravel
Vlad, 2017-10-04 16:08:05

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:

  1. In the app folder, I need to create a Repositories directory
  2. If I am working with users, I need to create a Users subdirectory
  3. In it, create a repository contract and implementation
  4. Bind the whole thing in the service provider
  5. Connect the contract to the controller in __construct and accept it in the private variable $users

According to various sources, this is how I imagine it. But there are also questions:
  1. How does all this look with AR?
  2. 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?
  3. Am I thinking right at all?

Thank you very much for your attention and future help. Do not consider the question banal or stupid, just want to figure it out in order to avoid mistakes in the future!

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
D3lphi, 2017-10-04
@Result007

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.

D
Dmitry Evgrafovich, 2017-10-05
@Tantacula

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.

H
hakkol, 2017-10-04
@hakkol

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...

I
Ivan, 2017-10-04
@IvanTheCrazy

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?

Those. it turns out that the all method of the repository will call the eloquent method all too? If the logic is something like this (and from the question it looks like that), then why have a repository at all?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question