How to Use Repository Design Pattern in Under 3 Minutes

In this post, we’re going to implement the Repository design pattern in a Laravel application.

If you’re coming from the Symfony world or any other framework that uses the Repository pattern to interact with the database, you’d be surprised to find out that Laravel doesn’t come with this functionality out of the box.

Even so, it’s still pretty easy to implement and use the Repository design pattern in a Laravel project.

Let’s go ahead and find out!

 

The API example

For the purpose of this blog, I’ve created an API that fetches all the users from the database.

In a classic Laravel application, you’d use the Eloquent Model to query the database directly in the Class where you write the code. It could be a Service type of Class or a Controller as in this case.

Now let’s refactor this piece of code so we can use the Repository design pattern.

Define an Interface

Let’s create a Repositories folder at the root of our application.

Before we create the actual Repository Class, we must first define an Interface to specify all the methods which the Repository must implement.

Instead of relying on the Repository Class, our Controller will depend on the Interface.

Let’s create a UserRepositoryInterface that has a public function getAll() that will return a Collection.



Create the Repository Class

Now that we have the Interface, let’s go ahead and create the Repository Class.

Inside the Repositories folder create a UserRepository Class which implements the UserRepositoryInterface.

Create the public function getAll() which returns a Collection and inside this method return all the users from the database.

Use the pattern in the Controller

Now that we have the UserRepository Class, let’s go ahead and use it in our Controller.

We’ll do that by using dependency injection.

And instead of relying on the Repository Class, our Controller will depend on the Interface.

In a real-world scenario, this will make the code more robust to changes because we rely on abstractions, not concretions, which is a key aspect of the Principle of Dependency Inversion.

Now let’s go ahead and see if our implementation is working.

And it doesn’t work because as I mentioned earlier, Laravel doesn’t support this out of the box.

UserRepository is not yet bound to UserRepositoryInterface, thus throwing an error

UserRepository is not yet bound to UserRepositoryInterface, thus throwing an error

Bind the Interface and the implementation

The last thing we need to do to make it work is to bind the UserRepository to UserRepositoryInterface in Laravel’s Service Container.

Let’s go ahead and create a new Service Provider by running the following command:

php artisan make:provider RepositoryServiceProvider

Open the RepositoryServiceProvider and inside the register() method add the following line of code.

And finally, we have to add the RepositoryServiceProvider to the providers array in the config/app.php file.


Let’s see if it works now…

And it does! Awesome!

JSON output of the API

JSON output of the API


I hope you are now a bit more familiar with the Repository design pattern and how you can use it in a Laravel application.

 

Let me know what you think about this article through comments below, or on Twitter at @pelu_carol.

If you find this article helpful, please share it with others and subscribe to the blog to support me, and receive a bi-monthly-ish e-mail notification on my latest articles.

 

You may also like...