The “Repository pattern” is essentially another level of abstraction on top of your database abstraction layer. In the context of Laravel, these are simple classes with methods that call more complex chains of methods on your model classes.

Generally, each repository is responsible for  one entity within your application. For example, a UserRepository should only be responsible for retrieving User records.

Abstract implementation

I’ve seen various people use traits to implement repositories directly inside of their models. Personally I think this gives the model too much responsibility, especially since models in Laravel are essentially “God classes” already.

Instead, I’ll create an  abstract class that all of the repository classes will extend:


namespace App\Repositories;

abstract class Repository

All of the repository classes will live inside of the app/Repositories folder and are namespaced accordingly. If you’re following a domain-driven design, you could put this class inside of a “shared” domain.


 Simple Repositories in Laravel
1.55 GEEK