<?php
|
|
namespace App\Traits;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
trait Service
|
{
|
/**
|
* Containing our repository to make all our database calls to
|
*/
|
protected $repository;
|
|
/**
|
* Get the record with the given id
|
*
|
* @param int $id
|
* @param bool $trashed
|
* @param array $relations
|
* @return \App\Models\User
|
* @throws \Exception
|
*/
|
public function get(int $id, $trashed = false, array $relations = [])
|
{
|
try {
|
return $this->repository->get($id, $trashed, $relations);
|
} catch (\Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* Update the record with the given id
|
*
|
* @param array $data
|
* @param Model $modelInstance
|
* @return mixed
|
* @throws \Exception
|
*/
|
public function update(array $data, Model $modelInstance)
|
{
|
try {
|
return $this->repository->update($data, $modelInstance);
|
} catch (\Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* @param array $params
|
* @param bool $onlyTrashed
|
* @param array $relations
|
* @param bool $paginate
|
* @param false $withTrashed
|
* @param null $groupBy
|
* @return mixed
|
* @throws \Exception
|
*/
|
public function search(array $params = [], bool $onlyTrashed = false, array $relations = [], bool $paginate = false, $withTrashed = false, $groupBy = null)
|
{
|
try {
|
return $this->repository->search($params, $onlyTrashed, $relations, $paginate, $withTrashed, $groupBy);
|
} catch (\Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* @param int $id
|
* @return mixed
|
* @throws \Exception
|
*/
|
public function destroy(int $id)
|
{
|
try {
|
return $this->repository->destroy($id);
|
} catch (\Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* @param int $id
|
* @return mixed
|
* @throws \Exception
|
*/
|
public function restore(int $id)
|
{
|
try {
|
$item = $this->get($id, true);
|
|
return $this->repository->restore($item);
|
} catch (\Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* @param array $params
|
* @return mixed
|
* @throws \Exception
|
*/
|
public function store(array $params)
|
{
|
try {
|
return $this->repository->store($params);
|
} catch (\Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* @param Model $model
|
* @param array $relations
|
* @return mixed
|
* @throws \Exception
|
*/
|
public function load(Model $model, array $relations)
|
{
|
try {
|
return $this->repository->load($model, $relations);
|
} catch (\Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* @return mixed
|
* @throws \Exception
|
*/
|
public function beginTransaction()
|
{
|
try {
|
return $this->repository->beginTransaction();
|
} catch (\Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* @return mixed
|
* @throws \Exception
|
*/
|
public function commitTransaction()
|
{
|
try {
|
return $this->repository->commitTransaction();
|
} catch (\Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* @return mixed
|
* @throws \Exception
|
*/
|
public function rollbackTransaction()
|
{
|
try {
|
return $this->repository->rollbackTransaction();
|
} catch (\Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* @param $attributes
|
* @param $params
|
* @return mixed
|
* @throws \Exception
|
*/
|
public function firstOrCreate($attributes, $params)
|
{
|
try {
|
return $this->repository->firstOrCreate($attributes, $params);
|
} catch (\Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* @param array $params
|
* @return mixed
|
* @throws \Exception
|
*/
|
public function deleteByCondition(array $params)
|
{
|
try {
|
return $this->repository->deleteByCondition($params);
|
} catch (\Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* @param $attributes
|
* @param $params
|
* @param bool $withTrashed
|
* @return mixed
|
* @throws \Exception
|
*/
|
public function updateOrCreate($attributes, $params, $withTrashed = false)
|
{
|
try {
|
return $this->repository->updateOrCreate($attributes, $params, $withTrashed);
|
} catch (\Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* @param Model $model
|
* @param $relation
|
* @param array $values
|
* @return mixed
|
* @throws \Exception
|
*/
|
public function syncRelations(Model $model, $relation, array $values)
|
{
|
try {
|
return $this->repository->syncRelations($model, $relation, $values);
|
} catch (\Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* @return mixed
|
*/
|
public function truncate()
|
{
|
return $this->repository->truncate();
|
}
|
}
|