Repository Pattern is an optimal design that helps you optimize, save code, and reuse in many places. In Laravel, it is between Model and Controller. This is where the data queries are executed. In the past you would usually write into controllor but you feel like you are iterating pretty much, especially the CRUD functions, almost every controller is quite similar. In this article, I will introduce to you the Repository to reduce code duplication in Controllers.
Question
If you have 2 controllers, Post and Category to have features to add, delete, edit … in these features, you just need to modify the Model, it is the same. So, you can completely deploy the Repository for it as follows
Deploy Repository
First, you create the Repositories folder on par with the app in your Laravel project. Inside I create an Eloquent folder (to contain Post and Category) and two files are EloquentInterface.php and EloquentRepository.php.
Inside EloquentInterface.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php namespace AppRepositories; interface EloquentInterface { public function all(); public function create(array $data); public function update(array $data, $id); public function delete($id); public function find($id); } |
Inside EloquentRepository.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | <?php namespace AppRepositories; use IlluminateDatabaseEloquentModelNotFoundException; abstract class EloquentRepository implements EloquentInterface { protected $_model; public function __construct() { $this->setModel(); } public function setModel() { $this->_model = app()->make( $this->getModel() ); } abstract public function getModel(); public function all() { return $this->_model->all(); } public function create(array $data) { return $this->_model->create($data); } public function update(array $data, $id) { return $this->find($id)->update($data); } public function delete($id) { return $this->find($id)->delete(); } public function find($id) { try { return $this->_model->findOrFail($id); } catch (ModelNotFoundException $e) { return false; } } } |
In EloquentRepository.php will have variable $ model received from the Repository model infused after the override method Eloquent getModel Inside the folder created above, I created another file is next 2 CategoryRepository.php and PostRepository.php
Content in CategoryRepository.php
I simply overwrite the getModel function and pass it to the Model Category
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php namespace AppRepositoriesEloquent; use AppCategory; use AppRepositoriesEloquentRepository; class CategoryRepository extends EloquentRepository { public function getModel() { return Category::class; } } |
Similarly, below is the PostRepository which will look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php namespace AppRepositoriesEloquent; use AppPost; use AppRepositoriesEloquentRepository; class PostRepository extends EloquentRepository { public function getModel() { return Post::class; } } |
How to use the created repositories
First, you need to go to AppServiceProvider.php as shown below and pass the register function code below
1 2 3 4 5 | $this->app->bind( 'AppRepositoriesEloquentInterface', 'AppRepositoriesEloquentRepository' ); |
So you have successfully declared the Repository, in the next step, you go to the Controller to use the created Repository. My example is CategoryController (you need to create CategoryController)
- Declare use App Repositories Model CategoryRepository; You add construct as below.
1 2 3 4 5 6 7 | protected $categoryRepo; public function __construct(CategoryRepository $category) { $this->categoryRepo = $category; } |
Or to create a new category, you can call it as shown below
In PostController you can do the same as on CategoryController.
Conclusion
The Repository Pattern is really effective and helps programmers a lot in reducing code repetition and being tighter in code lines. Hopefully, through this article, you can deploy Repository in your laravel project. If you have any questions, please comment below for me to discuss. Thank you for reading this article!