Using Repository in Laravel

Tram Ho

The Repository Pattern is very useful in Laravel, it makes it easy to maintain code, and minimizes the logic handling in the Controller. Repository is an abstraction , that is, it creates a layer between the logic and database processing in the application.

Repository is used to solve the separation when deploying an application, this is very important when writing code that makes it easy to maintain. For example, when our application is using MySQL and wants to change to MongoDB. Because we use the Repository Pattern, all the logic in our application is preserved and all we have to do is change the repository only. It sounds easier then right? ?

Let’s take a look at a simple example to better understand:

Step 1: Create a Repository Interface

We need an interface to act as a contract for the repository . So, what is contract?

Contract is simply like a written contract describing a specific job, here it is the interface .

Create a folder inside the app folder named Repositories .

Then create 1 PostRepositoryInterface.php file .

This is the interface that we will use. Inside this interface , you add the following code:

Now, we need to add contracts or methods so that PostRepository can implement later:

Note that there will be no closing and opening braces {}, because we do not implement the function in the interface (if you forget you can review the properties in OOP object-oriented programming), so there will be no The logic handling is in the interface, methods are only allowed to be declared.

Step 2: Create a Repository

We start creating the class . Inside the Repositories folder create PostRepository.php file This class will re-implement the PostRepositoryInterface interface that we just created in Step 1 .

The code in the PostRepository.php file is as follows:

Next, still in the PostRepository.php file, we are required to create methods declared in the interface and implement them, otherwise there will be errors during the run and will not use the class we have just created.

Here we will use the Eloquent Post model, so we must declare it to use:

Edit PostRepository.php as follows:

Thus, we have created an interface and a class using that interface . Now, we need to register the repository with the container in Laravel

Step 3: Declare Repository with Laravel IoC Container

Create a BackendServiceProvider.php file inside the Repositories folder. This file serves as a connection to the Laravel IoC Container and allows us to use dependency injection to add to our interface repository.

Code in BackendServiceProvider.php :

One thing to note here is the order between the interface and the class , you have to put the interface before the class you use, in this example, if you try to put AppRepositoriesPostRepository before AppRepositoriesPostRepositoryInterface , you will get an error, You must bind the interface first.

After the above steps, we will have a directory structure and files as follows:

Step 4: Use the Repository in the Controller

We start using the repository , assuming we already have a controller called PostController.php and this controller handles everything related to your Post.

All we have to do is add the interface to the controller through the constructor when the controller is initialized. This way we can use the repository for interactions with posts models :

The first we add the PostRepositoryInterface to the constructor

We then set a $ post variable to an instance of the PostRepository object through the interface . This allows us to call the PostRepository methods just like we used with the index () method, as you can see:

This prevents direct access to the model like Post::find($id) , and it adds us an abstract class into the application.

At the beginning of the article, we mentioned stopping using MySQL and using MongoDB instead, or any other technology, all you need to do is just change the logic of PostRepository.

That means that the code in our controller doesn’t need to be changed. ?

Step 5: Use more Repository

In an application you can add many different Repository. All you need to do is create an interface and then create a repository , then register them with the Laravel IoC Container in the Service Provider .

For example, we add a ** Comment Repository **

In the app directory:

Creating a CommentRepositoryInterface is similar to creating a PostRepositoryInterface in the previous steps.

CommentRepositoryInterface.php

CommentRepository.php

Finally, declare it in the IoC Container inside BackendServiceProvider.php .

Above is my find out about Repository in Laravel. Hope it can help you! ??

Share the news now

Source : Viblo