Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/omnicode/lara-service-model


https://github.com/omnicode/lara-service-model

Last synced: 10 days ago
JSON representation

Awesome Lists containing this project

README

        

# Lara-Service-Model

Generalized Service layer

## Installation

Run the following command from you terminal:

```bash
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
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
['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);
}
}

```