<?php
|
|
namespace App\Traits;
|
|
use Exception;
|
use Illuminate\Database\Eloquent\Model;
|
|
trait Repository
|
{
|
/**
|
* Eloquent model
|
*/
|
protected $modelInstance;
|
|
/**
|
* Get the record with the given id
|
*
|
* @param int $id
|
* @param bool $trashed
|
* @param array $relations
|
* @return Model
|
* @throws Exception
|
*/
|
public function get(int $id, bool $trashed = false, array $relations = []) : Model
|
{
|
try {
|
$modelInstance = ($trashed) ?
|
$this->modelInstance->withTrashed()->findOrFail($id) : $this->modelInstance->findOrFail($id);
|
|
if(!empty($relations)) {
|
$modelInstance->load($relations);
|
}
|
|
return $modelInstance;
|
} catch (Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* Update the record with the given id
|
*
|
* @param array $data
|
* @param Model $instance
|
* @return bool
|
* @throws Exception
|
*/
|
public function update(array $data, Model $instance)
|
{
|
try {
|
return $instance->update($data);
|
} catch (Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* @param array $params
|
* @param bool $onlyTrashed
|
* @param array $relations
|
* @param bool $paginate
|
* @param bool $withTrashed
|
* @return mixed
|
* @throws Exception
|
*/
|
public function search(array $params = [], bool $onlyTrashed = false, array $relations = [], bool $paginate = false, $withTrashed = false, $groupBy = null)
|
{
|
try {
|
$query = $this->modelInstance::searchWithParams($params);
|
|
if($withTrashed) {
|
$query->withTrashed();
|
} elseif($onlyTrashed) {
|
$query->onlyTrashed();
|
}
|
|
if(!empty($relations)) {
|
$query->with($relations);
|
}
|
|
if(!empty($groupBy)) {
|
$query->groupBy($groupBy);
|
}
|
|
return ($paginate) ? $query->paginate($this->modelInstance->perPage) : $query->get();
|
} catch (Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* @param int $id
|
* @return bool|null
|
* @throws Exception
|
*/
|
public function destroy(int $id)
|
{
|
try {
|
return $this->get($id)->delete();
|
} catch (Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* @param Model $instance
|
* @return bool|null
|
* @throws Exception
|
*/
|
public function restore(Model $instance)
|
{
|
try {
|
return $instance->restore();
|
} catch (Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* @param array $params
|
* @return mixed
|
* @throws Exception
|
*/
|
public function store(array $params)
|
{
|
try {
|
return $this->modelInstance->create($params);
|
} catch (Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* @throws Exception
|
*/
|
public function beginTransaction()
|
{
|
try {
|
\DB::beginTransaction();
|
} catch (Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* @throws Exception
|
*/
|
public function commitTransaction()
|
{
|
try {
|
\DB::commit();
|
} catch (Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* @throws Exception
|
*/
|
public function rollbackTransaction()
|
{
|
try {
|
\DB::rollback();
|
} catch (Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* @param Model $model
|
* @param array $relations
|
* @return Model
|
* @throws Exception
|
*/
|
public function load(Model $model, array $relations)
|
{
|
try {
|
$model->load($relations);
|
|
return $model;
|
} catch (Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* @param array $attributes
|
* @param array $params
|
* @return mixed
|
* @throws Exception
|
*/
|
public function firstOrCreate(array $attributes, array $params)
|
{
|
try {
|
return $this->modelInstance->firstOrCreate($attributes, $params);
|
} catch (\Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* @param array $params
|
* @return mixed
|
* @throws Exception
|
*/
|
public function deleteByCondition(array $params)
|
{
|
try {
|
$queryBuilder = $this->modelInstance;
|
foreach($params as $fields => $value) {
|
$queryBuilder = $queryBuilder->where($fields, "=", $value);
|
}
|
return $queryBuilder->delete();
|
} catch (\Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* @param array $attributes
|
* @param array $params
|
* @param bool $withTrashed
|
* @return mixed
|
* @throws Exception
|
*/
|
public function updateOrCreate(array $attributes, array $params, $withTrashed = false)
|
{
|
try {
|
return ($withTrashed) ? $this->modelInstance->withTrashed()->updateOrCreate($attributes, $params)
|
: $this->modelInstance->updateOrCreate($attributes, $params);
|
} catch (\Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* @param Model $model
|
* @param $relation
|
* @param $values
|
* @return mixed
|
* @throws Exception
|
*/
|
public function syncRelations(Model $model, $relation, array $values)
|
{
|
try {
|
return $model->{$relation}()->sync($values);
|
} catch (\Exception $e) {
|
throw ($e);
|
}
|
}
|
|
/**
|
* @throws Exception
|
*/
|
public function truncate() {
|
try {
|
$this->modelInstance->whereNotNull('id')->delete();
|
} catch (\Exception $e) {
|
throw($e);
|
}
|
}
|
}
|