Sample implementation of repository in Laravel for large scale application
The main purpose of Repositories is to segregate and create a layer between the controllers and database query layer(Eloquent) to keep the codebase clean and well organized which is most required in a large applications.
We can implement interface to put a layer in between the controllers and the transformer model that will give us flexibility
We can also implement decorators to transform the data and re-utilize the transformer the way we intend to
- Keep the code base well-managed and organized.
- Allows re-usability of the methods and queries keeping dry principle in play.
- Creating a layer between the controller and Database query by utilizing interface makes it easy to change to any type of DB in future if we need and think is efficient. Thus providing flexibility.
- Implementing Abstract repository class will allow to reuse the repo for different models along with that data obtained can be modified the way required by using decorators