Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pstephan1187's avatar

What is the difference between a model and a repository

I know what a model is and am fluent in using them, but what exactly is a repo, how does it differ from a model, and why would I use it over a model?

0 likes
13 replies
JarekTkaczyk's avatar

@pstephan1187 Some say, you should use the repository, because it allows abstraction from the data provider engine. Some say, you should do it because repository is testable. Others use it, because it is trendy ;)

While all of the above is true in a way, I suggest you read this short article @ Fowler's and you should get the idea http://martinfowler.com/eaaCatalog/repository.html

3 likes
michaeldyrynda's avatar

The repository basically allows you to decouple your business logic from your database layer.

Whilst your eloquent model knows how to find all records for a given entity, your repository could wrap that in business-specific logic:

// Eloquent
$users = User::where('active', true)->get();

// Repository
$users = $this->userRepository->getAll(true);

class UserRepository {
    protected $model;

    public function __construct(User $model)
    {
        $this->model = $model;
    }

    public function getAll($active = false)
    {
        if ( $active === true )
        {
            return $this->model->where('active', true)->get();
        }

        return $this->model->all();
    }
}

This gives you testability and separation of concerns, your app is no longer tightly coupled to a particular implementation. In doing so, it becomes simpler to switch out the eloquent persistence for talking to a JSON API for example. This becomes easier still when you then you bind an interface to your controllers - something like UserRepositoryInterface - because you can update all uses of that interface just by updating one line in your service provider.

As with anything, though, it's up to you to determine if this is appropriate for your application. Repositories add another layer of complexity to your app that perhaps a small CRUD application won't actually need.

12 likes
JeffreyWay's avatar

There are lots of little benefits to using repositories. It's never one specific reason.

If nothing else, a big win is the ability to take a complicated query, and isolate it behind a readable method name. Things like Post::all() are great for demos, but they don't really reflect real life. Instead, you'll have various query scopes, where clauses, joins, orders, etc. It's not fun to type all of that out in your controller over and over.

A repository gives you a nice place to put these complex queries, while still providing a clean API to use from your controller, service, whatever.

24 likes
pstephan1187's avatar

Thanks all for the responses. They really help me understand their purpose. Very good information. The RepositoryInterface implementation really makes a lot of sense, as does the separation of business logic.

1 like
andy's avatar

I'm not really a best practices person so take this with a grain of salt. Also, I'm an old dog trying to learn new tricks.

I totally agree that a controller shouldn't have DB calls in it. So, I push all of that all into my model. You end up with Post:getAll() instead of Post:all() ...

I'm coding only for mysql and not for the ability to swap out to Oracle or Postgres on the fly, so I don't see much reasoning behind having to jump through more files to find my db calls. However, I should actually code based on tests which would make repositories life savers.

The one thing I've been wondering about is what is the hit on your system for using repositories vs models vs dirty ugly looking controllers?

2 likes
pmall's avatar

I'm coding only for mysql and not for the ability to swap out to Oracle or Postgres on the fly

It is not the responsibility of the repository but of its underlying db service. A more meaningful example would be switching from mysql to mongodb.

The one thing I've been wondering about is what is the hit on your system for using repositories vs models vs dirty ugly looking controllers?

Negligible

1 like
andy's avatar

@ pmall Thanks for the info.

1 like
kadnan's avatar

I was confused untill I read the response by @xroot which really made sense otherwise the Model itself acts as an abstraction layer by using an ORM.

Fawwad's avatar

@kadnan i am also agree with you. any ways but thanks @ michaeldyrynda for sharing.

Please or to participate in this conversation.