Learn and deploy the Repository Pattern in Laravel

Tram Ho


Laravel is a free and open source PHP framework, built to support and develop software and applications, following the MVC architecture. Currently, Laravel is the most popular and best PHP framework. One of the most popular ways to build an application using Laravel is the Repository Pattern. First, to be able to deploy the Repository Pattern in Laravel we need to understand what a Repository Pattern is and the benefits of using it.

What is Repository Pattern?

The Repository Pattern is a way of organizing source code in Laravel. The basic idea and main goal to use the Repository Pattern is to build a connection layer between the Business Login layer and Data Access, making data access more strict and secure.

In Laravel, the Repository is the bridge between the Model and the Controller, storing the data querying logic. Data query commands that are written directly at the Controller will be put into Repository. Then, Controller will use Repository to interact with data instead of using it directly. The query of data is hidden in Repository. Normally in Laravel, methods like find, create, update, delete .. are written quite similarly, only with different on Model (object) need interaction. So these codes should be put into Repository to save coding.

Benefits of using the Repository Pattern

  • Centralization of the data query logic makes the code easier to maintain
  • The data query logic and the code logic can be separated
  • Reduce code duplication
  • Limit errors in the query
  • Easy to perform test

Usually when you don’t use Repository, the code will look like this

This code is still correct. However, there is a problem to note here is that the Controller is attached to the Model used to perform operations with the database. This causes 2 serious problems:

  • We cannot write Test for Controller
  • The controller is tied to the ORM Layer, if there are changes to the table structure, or implement some refactoring solution, we will have a hard time having to find all the controllers and fix them.

That is why we should use Repository in this case. We will build an intermediate layer between Controller and Model, instead of the Controller interacting directly with the Model, the Controller will work with the repackaged Repository and manipulate the Model. With any changes related to Model, we just need to find and modify the Repository. We’ll rewrite the code as follows:

Use Repository in Laravel

After learning about the benefits of Repository, now let’s deploy Repository in Laravel.

Step 1: Create a Repository Interface

Create a folder inside the app folder named Contracts , and then create the Repositories folder with the following structure

Then create the file BaseRepository.php This is the Interface that we will use. Inside this interface, add the following code:

Next, in the UserRepository.php file, inherit BaseRepository.php

Step 2: Create Repository

Create a folder inside the app folder named Repositories and create an Eloquents folder with the following structure:

Eloquents folder created inside EloquentBaseRepository.php file implements the interface class will be BaseRepositoryInterface that we just created in Step 1. Code in EloquentBaseRepository.php file as follows:

In the EloquentBaseRepository.php file, we are required to create methods declared in the interface and deploy them, otherwise, there will be an error during the run and will not be able to use the class we just created. Next, create the file EloquentUserRepository inheriting EloquentBaseRepository and this class will implement the UserRepositoryInterface interface that we just created in Step 1. The code in the file EloquentUserRepository.php is as follows:

Step 3: Register in app / Providers / AppServiceProvider.php

Step 4: Use Repository in Controller


Now you can successfully deploy the Repository in Laravel. In an application you can also deploy many different Repositories, all you need to do is create an interface and then create a repository, then register them in the Service Provider. Above is my study about Repository in Laravel. . Hope it can be of help

Share the news now

Source : Viblo