Repository Design Pattern Implementation in Laravel — Using Laravository Package

source: https://www.forbes.com/sites/enriquedans/2020/08/09/could-the-no-code-movement-put-programmers-out-of-ajob/

What is Repository Design Pattern?
Repository Design Pattern is a kind of container where data access logic is stored. It hides the details of data access logic from business logic. In other words, we allow business logic to access the data object without having knowledge of the underlying data access architecture.

source: https://techtalk.id/Repository-design-pattern

Why we should Repository Design Pattern?
The main idea to use Repository Pattern in a Laravel application is to create a bridge between models and controllers. In other words, to decouple the hard dependencies of models from the controllers. The model should not be responsible for communicating with or extracting data from the database.

And why Laravository package?
With this package, it allows us to create the Repository Class and the Interface in one line of command. So, we don't need to do the same thing repeatedly.

There are many approaches to implement Repository Design Pattern.
In this article, we will use a Laravel Package named Laravository.

Before we do the implementation, we need to setup our package.
First, install the Laravository to our laravel project from the composer. run this composer command.

Then, we need to publish the Provider. run this artisan command below.

After the provider got published, we need to register class to the

That’s it, our setup to implement design completed.

Now, we will do the implementation of .
First, we can use the Laravository command to create the Repository Class and the Interface like this.

It will generate and The Repository class would look like this.

UserRepository Class
UserRepositoryInterface Interface

Before we can use this . we need to register it to the RepositoryServiceProvider. Like this example below.

RepositoryServiceProvider.php

Now, finally. We can call the interface in our controller.

UserController.php

Maybe, you will ask me where does the method comes from?
So, The BaseRepository itself has default methods to do some default stuff. like Getting data, create data, update data, and delete data.

Here are lists of the default methods.

Software Engineer