The Repository Pattern is a software design pattern commonly used in object-oriented programming to separate the logic that retrieves and stores data from the rest of the application. It provides a way to abstract and encapsulate data access operations, making the code more modular, maintainable, and testable.
In the Repository Pattern, a repository acts as an intermediary between the data source (such as a database or web service) and the application. It provides a consistent interface for performing CRUD (Create, Read, Update, Delete) operations on data objects without exposing the underlying data access details.
By using the Repository Pattern, developers can isolate the data access code from the business logic, promoting separation of concerns and making it easier to swap out data sources or change the underlying data access technology. It also allows for better testability by providing a way to mock or substitute repositories during unit testing.
Overall, the Repository Pattern improves code organization, flexibility, and maintainability by abstracting data access operations and promoting modular design in software applications.
A simple Laravel application based on REPOSITORY PATTERN, starter kit to kick start development using Laravel.
Application Name ................................................................... Repository Pattern in Laravel
Laravel Version ...................................................................... 10.4.1
PHP Version ........................................................................... 8.1.10
Database ................................................................................ MYSQL
Customers (Create | Read | Update (PUT, PATCH) | Delete).
Orders (Create | Read | Update (PUT, PATCH) | Delete)
Go to CLI and run below commands:
https://github.com/imgrasooldev/repository-pattern-in-laravel.git
cd repository-rattern-in-laravel
open .env file (available) at root directory and check (or update) database details.
using CLI run below commands also:
composer install
php artisan key:generate
php artisan migrate:fresh --seed
php artisan serve
POST /api/v1/register
Body => {
"name": "",
"email": "",
"password": "",
"confirmPassword": ""
}
POST /api/v1/login
Body => {
"email": "",
"password": ""
}
(Pass token from login/register response as bearer-token to all below apis as all are restricted with sanctum middleware)
GET /api/v1/customers
GET /api/v1/customers/{id}
GET /api/v1/customers?includeOrders=true&name[eq]=Rubye Lemke
"IncludeOrders query parameter is to include individual customer orders inside response."
POST /api/v1/customers
Body => {
"name": "",
"email": "",
"address": "",
"city": "",
"state": "",
"postalCode": ""
}
PUT /api/v1/customers/{id}
Body => {
"name": "",
"email": "",
"address": "",
"city": "",
"state": "",
"postalCode": ""
}
PATCH /api/v1/customers/{id}
Body => {
"name": "",
}
DELETE /api/v1/customers/{id}
GET /api/v1/orders
GET /api/v1/orders/{id}
GET /api/v1/orders?includeCustomer=true&isFulFilled[eq]=1
"IncludeCustomer query parameter is to include customer against order in response."
POST /api/v1/orders
Body => {
"customerId": "131",
"details": "Lorem is simply a dummy text",
"isFulFilled": 0
}
PUT /api/v1/orders/{id}
Body => {
"customerId" : 132,
"details" : "adafABCDEFghijkls222",
"isFulFilled" : 1
}
PATCH /api/v1/orders/{id}
Body => {
"customerId" : 132,
"details": "Lorem ipsum ABCDEFghijkl"
}
DELETE /api/v1/orders/{id}
Best of Luck 👍
Rate my work please ⭐