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 */

TrackBack URI

Leave a comment

Name: (Required)

eMail: (Required)