Downloads
Stars
Version
As Repositories layer uses laravel-repositories library.
Install the saritasa/laravel-entity-services
package:
bash $ composer require saritasa/laravel-entity-services ```
## Usage
### Get service for model:
```php
$entityServiceFactory = app(IEntityServiceFactory::class);
$entityService = $entityServiceFactory->build(User::class);
*Note: if entity class not exists, EntityServiceException will be thrown
To publish configuration file you can run next command:
php artisan vendor:publish --tag=laravel_entity_services
It will copy file laravel_entity_services.php in config directory.
To register your own IEntityService implementation you can put it into configuration file, like:
return [
'bindings' => [\App\Models\User::class => \App\EntityServices\UserEntityService::class,],];
NOTE: Just remember that default IEntityServiceFactory implementation can work only with classes extended from EntityService. If you want change this behavior you should add your own implementation.
$createdModel = $entityService->create($params);
php $entityService->update($model, $params);
php $entityService->delete($model);
If you need use custom service for some entity, you can register it in factory using register
method.
Example:
$entityServiceFactory = app(IEntityServiceFactory::class);
$entityService = $entityServiceFactory->register(User::class, YourServiceRealization::class);
Note: Your realization must be extend EntityService class
EntityCreatedEvent - Throws when entity is created.
EntityUpdatedEvent - Throws when entity is updated.
EntityDeletedEvent - Throws when entity is deleted.
If you have GNU Make installed, you can use following shortcuts:
make cs
(instead of php vendor/bin/phpcs
) -make csfix
(instead of php vendor/bin/phpcbf
) -make test
(instead of php vendor/bin/phpunit
) -make install
- instead of composer install
* make all
or just make
without parameters -