With easy repository, you can have the power of the repository pattern, without having to write too much code altogether. The package automatically binds the interfaces to the implementations, all you have to do is change in the configuration which implementation is being used at the moment!
- Minimum PHP ^8.1
You can install the package via composer
$ composer require yaza/laravel-repository-service
Publish the config file with (Important):
php artisan vendor:publish --provider="LaravelEasyRepository\LaravelEasyRepositoryServiceProvider" --tag="easy-repository-config"
You can also create only the repository, or service, or both with artisan:
php artisan make:repository User
// or
php artisan make:repository UserRepository
// or create together with a service
php artisan make:repository User --service
// or
php artisan make:repository UserRepository --service
// or create a service separately
php artisan make:service User
// or
php artisan make:service UserService
// or
php artisan make:service UserService --repository
// create service for api template
php artisan make:service UserService --api
This package support for generate Service & Repository pattern with artisan command :
- On service you can write bussines logic
- On Repository you can write a query logic for database
if you use command php artisan make:service or repository, this command will generate 2 file:
- interface
- implement class
interface auto bind to class implement with container laravel, for detail you can read read docs bind interface with implement you can call method on interface to access method in class implement.
if you need to change or modification bind interface to new implement class you can add this config to AppServiceProvider :
$this->app->extend(Interface::class, function ($service, $app) {
return new NewImplement($service);
});
class implement extend with service or Elequent for handel basic method CRUD like, create, update, delete, findOrFail
you can override basic method crud for re-write code logic
-
Controller
you can call interface of service with automatic injection depedency read more detail controller only call interface of service not class implement of service -
Service
sample in interface of service, the interface bind to class implementation sample code in class implementation class implement service only call interface of repository with automatic injection depedency , service not recommended direct call method on class implement of repository -
Repository
sample interface of repository sample code implement of repository, implement repository extend with basic CRUD query logic -
implement class of service api diferent implement service with implement service api only on extend class
-
on your controller need call ->toJson() end of method service to auto generate response json from ServiceApi extends class
public function methodNameOnController() {
return $this->nameOfService->methodOfService()->toJson();
}
output or response with extend ServiceApi, more detail you can read code on ServiceApi
```json
{
"success": true,
"code": 200,
"message": 'Your message',
"data": [
{
"id": 1,
"name": "tes",
"email": "[email protected]",
"email_verified_at": null,
"created_at": null,
"updated_at": null
}
]
}
Please see CHANGELOG for more information on what has changed recently.
The MIT License (MIT). Please see License File for more information.