A repository provides separation between a domain and a persistent layer. A repository provides an interface to access data stored in the database or external resources. Data is returned in the form of objects.
Repositories act as a bridge between the models and the controller. By using Repository Pattern we can decouple the hard dependencies of models from the controllers. The model should not be responsible for communicating with or extracting data from the database. Therefore, to keep your Laravel code clean and safe, we need to use repositories that will separate the responsibilities for which the model should never be responsible.
The use of Repository Pattern has many benefits, some of them are given below:
- Due to the centralization of the data access logic, it makes code easier to maintain.
- Business & data access logic can be tested separately.
- Reduces duplication of code
- A lower chance for making programming errors
Generally, we are writing code in this format.
When we write code using this way, later on, change may turn out to be very difficult to implement, or even impossible. Every implementation should be based on interfaces. So later, if you want to perform changes you don’t need to change the code in the whole application, but only create another class implementing the interface. This should be the code above written in the correct way.
In this example, if a client wants to change the data structure, We will simply create a class that implements UserRepositoryInterface and contains all the changes and everything works again.
Let’s Start with code:
We should start by creating a Repository folder into our app folder and one more folder named Eloquent. So the folder structure should look like this:
EloquentRepositoryInterface.php
BaseRepository.php:
So that is our base repository. We will inject model classes into the constructor and store methods that may be used in every eloquent repository in this current example. It will be just created() and find() methods.
UserRepositoryInterface.php
The next step is to implement our user repository using the parent class/interface.
UserRepository.php
And that’s it! We have implemented a repository pattern in laravel.To let our application know which implementation of which interface we want to use we need to create a Laravel service provider. We will name it RepositoryServiceProvider
After creating this file let’s open it (App/Providers/RepositoryServiceProvider.php) and add some new line of codes:
Now register this service provider in our config/app.php.
Add below line to add RepositoryServiceProvider in our providers
App\Providers\RepositoryServiceProvider::class.
Now our application knows what class it should use when we type an object by its interfaces. So we can simply use making logic like this in the controller.
This application will work the same way as before even if the data engine has changed and we didn’t need to change even 1 line of code in our controller or anywhere else!
If you have skills in PHP programming and you want to enhance your career in this field, a PHP certification from StudySection can help you reach your desired goals. Both beginner level and expert level PHP Certification Exams are offered by StudySection along with other programming certification exams.