DEV Community

Discussion on: Laravel Repository Pattern

Collapse
 
andreich1980 profile image
AndrewP

When people advocate for repositories pattern they often say "what if I want to change data storage from MySQL to something else". And it always makes me wonder how many times have you heard of such a decision if ever?

Here you're creating a s***load of additional files (a repo, a base repo, an interface, a service provider) just in case that might never happen.

You have a method find to find a model by id. What if you need to get a model by other attributes (email, name)? Would you create a couple of new methods like findByXxx in the repo and in the interface?

Collapse
 
mabalashov profile image
mabalashov • Edited

"To change data storage" - is not the main aim of Repository.
The main aim is to have separate layers and clean code. It becomes much easier to make scalable code, cover it by tests if you are going this way. But it makes sense to use repositories and follow the best practices of clear code if you are developing big enterprise applications.
For RAD the ActiveRecord (Eloquent) is the best

But, in the current article author suggests using Eloquent Models with Repository. It is not the best way and it will be better to use or AR or Repo. Otherwise, you will lose all the benefits of both ones.