Laravel repository pattern explanation Laravel repository pattern explanation laravel laravel

Laravel repository pattern explanation


Question 1

Laravel uses ActiveRecord as pattern to its models + data access layer.

This pattern has as objective make things easier and faster. When you use a Repository pattern, you are kind of running away from that.

To use for real the repository pattern, you would have to create data transfer objects, DAT (in java they call it POJO). And then, use the Eloquent entity just as an Entity Manager.

POPO

class User {  private $id;  private $a;  public setId ($id) { $this->id = $id; }   public getId () { return $this->id; }   public setA ($a) { $this->a = $a; }   public getA () { return $this->a; } }

Entity Manager

class UserEloquent extends Eloquent {   protected $_table = 'user';}

Repository

class EloquentUserRepository implements UserReposistory {   private $em;   public __constructor (UserEloquent $em) { $this->em = $em; }   public update (User $dat) {     $user  = $em->find($dat->getId());     $user->a = $dat->getA();     $user->save();   }}

Can you see how verbose would it be? If you really want to use this approach, I recommend you to use Doctrine.

But if you want to use ActiveRecord as it is, I recommend you to see some ruby on rails projects! They are really good in this pattern :)

Now what I really think: make what is good for the project. Make it simple, think with your team and build the best practices for you. Don't use always the same pattern because someone told you.

If the project is small and fast, use what the framework gives you (I'm not saying to relax and make bad code). If it's a big project, with a lot of developers in the team and you know that eventually you may want to change your Data Access Layer, think about Repository Pattern.

Of course sometimes we can take wrong architecture decisions, but it is how we learn! And keep reading about patterns, I think you are following a good path to be a great Architect :)

Question 2

It depends in what you've decided above. If its Eloquent using ActiveRecord as it is, doesn't matter where to call it (Controller or some middle class). Just follow some pattern. If you call in Controller always do it in Controller. Else you'll get lost in your code.

Using Repository, I think inside the repository it is fine :)