Why the Repository Pattern is Often Misunderstood in the Laravel Community
Nick Lee
71

Interesting article. Working on an approach myself (https://github.com/sebastiaanluca/laravel-modules/tree/develop/src/Repositories) that converts Eloquent results from a repository to basic value objects that contain nothing but the data itself. Somewhat like Doctrine entities, but with standard PHPDoc and with the benefit of active record method chaining to build a query.

This enables code completion in IDEs (Eloquent models are no source of truth, they just contain the logic for CRUD operations and data manipulation) and enforces repositories as the only way to retrieve data and perform database operations. Since it’s based on Rinvex repositories caching can be enabled on repository level, drastically speeding up your application.

Should you feel the need to switch ORMs (I doubt anyone ever has to when a project has already started), you can do so by implementing a new repository type and binding it. All without affecting your application. Only drawback here is I like active record queries so much, you’d basically have to reimplement method chaining for i.e. Doctrine so it adheres to the repository interface.