omnicode / lara-service-model
Laravel Service Model layer for CRUD.
Installs: 5 423
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=7.1
- ext-json: *
- laravel/framework: 5.8.*
- omnicode/lara-model: ^5.0
- omnicode/lara-tools: ^0.1.0
- omnicode/lara-validation: ^5.0
- omnicode/php-util: ^5.0
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: ^4.8.35 || ^5.7 || ^6.4 || ^7.0
- dev-master
- v5.x-dev
- 5.0.2
- 5.0.1
- 5.0.0
- v4.x-dev
- 4.1.3
- 4.1.2
- 4.1.1
- 4.1.0
- 4.0.20
- 4.0.19
- 4.0.18
- 4.0.17
- 4.0.16
- 4.0.15
- 4.0.14
- 4.0.13
- 4.0.12
- 4.0.11
- 4.0.10
- 4.0.9
- 4.0.8
- 4.0.7
- 4.0.6
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- v3.x-dev
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.22
- 3.0.21
- 3.0.20
- 3.0.19
- 3.0.18
- 3.0.17
- 3.0.16
- 3.0.15
- 3.0.14
- 3.0.13
- 3.0.12
- 3.0.11
- 3.0.10
- 3.0.9
- 3.0.8
- 3.0.7
- 3.0.6
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
This package is auto-updated.
Last update: 2024-12-04 23:02:30 UTC
README
Generalized Service layer
Installation
Run the following command from you terminal:
composer require "omnicode/lara-service-model: 3.0.*"
or add this to require section in your composer.json file:
"omnicode/lara-service-model": "3.0.*"
then run composer update
Usage
First, create your Service class like shown below with example UserService
<?php namespace App\Services; use App\Models\User; use App\Validators\UserValidator; use LaraServiceModel\LaraServiceModel; /** * Class UserService * @package App\Services */ class UserService extends LaraServiceModel { public function __construct(User $userModel) { parent::__construct($userModel); } }
To implement the validation used LaraValidation. And finally, use the service and validator in the controller:
<?php namespace App\Services; use App\Models\User; use App\Validators\UserValidator; use LaraServiceModel\LaraServiceModel; /** * Class UserService * @package App\Services */ class UserService extends LaraServiceModel { public function __construct(User $userModel, UserValidator $userValidator) { parent::__construct($userModel, $userValidator); } }
Available Methods
The following methods are available in LaraServiceModel:
$this->setBaseModel($userModel); $this->setBaseValidator($userValidator); $this->all($columns = null): Collection; $this->paginate(int $count = 20, $columns = null, $group = 'list'): array; $this->simplePaginate(int $count = 20, $columns = null): Paginator; $this->create(array $data, string $ruleValidate = 'default'); $this->createMany(array $data, $useTransaction = false): bool; $this->createWith(array $data, $relations, string $ruleValidate = 'default'); $this->update(array $data, $id, string $ruleValidate = 'default'); $this->updateBy(string $column, $value, array $data); $this->updateWith(array $data, $id, $relations = null, $rule = 'default'); $this->delete(int $id, string $deletedMethod = 'delete'); $this->deleteBy(string $column, int $id, string $deletedMethod = 'delete'); $this->deleteAll(); // Use caution (you can delete all data from the table) $this->destroy(int $id, string $deletedMethod = 'delete'); $this->destroyAll(); // Use caution (you can delete all data from the table) $this->first($columns = null); $this->last($columns = null); $this->find($id, $columns = null); $this->findBy(string $attribute, $value, $columns = null); $this->findForShow($id, $columns = null); $this->findAllBy(string $attribute, $value, $columns = null); $this->findList(?array $listable = null); $this->findListBy(string $attribute, $value, ?array $listable = null); $this->findCount(?string $attribute = null, $cmpOrValue = '=', $value = null); $this->increment(string $column, $value = 1); $this->decrement(string $column, $value = 1); $this->pushWhere($column, $cmp = '=', $value = null); $this->pushOrWhere($column, $cmp = '=', $value = null); $this->pushWhereNull($column); $this->pushWhereNotNull($column); $this->pushHas(string $relation, $cmpOrValue = '=', $value = null); $this->pushWhereHas(string $relation, $condition); $this->pushOrWhereHas(string $relation, $condition); $this->pushDoesntHave(string $relation); $this->pushWhereDoesntHave(string $relation, $condition); $this->pushOrderBy(string $column, $order = 'asc'); $this->pushLimit(int $limit); $this->pushOffset(int $count); $this->pushSkip(int $count); $this->pushTake(int $count); $this->pushHaving($column, $cmpOrValue = '=', $value = null); $this->pushGroupBy(...$columns); $this->pushSelect($columns); $this->pushWhereBetween(string $column, array $values); $this->pushWhereNotBetween(string $column, array $values); $this->pushWhereIn(string $column, array $values); $this->pushWhereNotIn(string $column, array $values); $this->pushSearch($column, $search = null); $this->pushWhereDate(string $column, string $value); $this->pushWhereMonth(string $column, string $value); $this->pushWhereDay(string $column, string $value); $this->pushWhereTime(string $column, string $value); $this->pushWith($with); $this->pushWithCount($with); $this->setValidationErrors($errors); $this->getValidationErrors(); $this->startTransaction(); $this->commitTransaction(); $this->rollbackTransaction();
Usage examples
<?php namespace App\Services; use App\Models\Article; use App\Validators\ArticleValidator; use LaraServiceModel\LaraServiceModel; /** * Class ArticleService * @package App\Services */ class ArticleService extends LaraServiceModel { /** * ArticleService constructor. * @param Article $articleModel * @param ArticleValidator $articleValidator */ public function __construct(Article $articleModel, ArticleValidator $articleValidator) { parent::__construct($articleModel, $articleValidator); } /** * @return mixed */ public function index() { $relatedCount = [ 'category' => ['name', '=', 'name'], 'images' ]; $this->pushWithCount($relatedCount); return $this->paginate(); } /** * @param int $id * @return mixed */ public function show(int $id) { $related = [ 'category' => [ 'pushSelect' => [ 'id', 'article_id', 'name' ], 'pushLimit' => 5, 'pusWhere' => [ ['status', 'active'], ['children', '>', 5] ] ] ]; $columns = [ 'id', 'title', 'description', 'keywords', 'name', 'header', 'content', 'views' ]; return $this->pushSearch('name', 'article name') ->pushWhere('views', '>', 5) ->pushWith($related) ->find($id, $columns); } /** * @param array $data * @return bool|mixed */ public function createArticle(array $data) { return $this->create($data, 'customValidatorName'); } /** * @param array $data * @return mixed */ public function createArticleAndRelations(array $data) { $data = [ 'title' => 'article title', 'description' => 'article description', 'keywords' => 'article keywords', 'name' => 'article name', 'header' => 'article header', 'content' => 'article content', 'images' => [ // HasMany relation ['url' => 'url one'], ['url' => 'url two'] ], 'categories_ids' => [ // BelongsToMany relation 1, 2, 15 ], 'type' => [ // HasOne relation 'type_name' => 'article type name' ] ]; return $this->createWith($data, ['images', 'categories']); } /** * @param int $id * @return mixed */ public function deleteArticle(int $id) { return $this->delete($id); } /** * @param string $slug * @return mixed */ public function deleteArticleBySlug(string $slug) { return $this->deleteBy('slug', $slug); } }