Laravel Repository Pattern Experience
I am always trying to learn, and lately I’ve been working on the repository pattern via Laravel. Â I found myself WAY overcomplicating this, and it turned into a mess.
With that said, I think I got myself to a very happy point. Â All of the examples on the Internet that I have found ultimately return Eloquent models. Â Which, if your controller is ONLY accessing the data, is fine. Â But I found myself then relying on Eloquent, which defeated the purpose of the repository.
Introducing domain objects. Â I was trying so hard to use active record objects, that it ended up causing me issues. Â In the end, I want controllers to be very small and ask a domain object for the data it needs to pass back to the requestor (Web visitor, or API consumer).
The domain object is responsible for all logic (not data). Â The domain object should interact with the repositories, and ask for data, or send data.
The repositories are only responsible for retrieving data or saving data. Â They should return either arrays or plain old php objects (popo), not Eloquent, Mongo, Redis, etc… models.
Controllers ask the domain to DO something. Â The domain asks the repositories for data it may need to do said something. Â The domain asks the repositories to store some data.
Once I started following these rules, things started making sense to me.
/** @todo: show examples */