Files

94 lines
3.0 KiB
PHP

<?php
namespace App\Repositories\Service;
use App\Models\Customer;
use App\Repositories\BaseRepository;
use App\Models\Service;
class ServiceRepository extends BaseRepository implements ServiceRepositoryInterface
{
public function getModel()
{
return Service::class;
}
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
{
$model = $this->model->with($relationships)->select($select);
if (isset($data['customer_uuid'])) {
$customer = Customer::where('uuid', $data['customer_uuid'])->first();
if ($customer) {
$model = $model->where('customer_id', $customer->id);
}
} elseif (isset($data['customer_id'])) {
$model = $model->where('customer_id', $data['customer_id']);
}
if (isset($data['status'])) {
$model = $model->where('status', $data['status']);
}
if (isset($data['product_id'])) {
$model = $model->where('product_id', $data['product_id']);
}
if (count($withCount) > 0) {
$model = $model->withCount($withCount);
}
if (isset($data['page'])) {
return $model->paginate(15);
}
return $model->get();
}
public function findWithRelationships(int $id = 0, array $relationships = [], array $withCount = [], array $withSum = [])
{
$model = $this->model;
if (count($relationships) > 0) {
foreach ($relationships as $relationship) {
if (method_exists($model, $relationship)) {
$model = $model->with($relationship);
}
}
}
if (count($withCount) > 0) {
foreach ($withCount as $key => $items) {
if (is_array($items)) {
foreach ($items as $alias => $item) {
$model = $model->withCount([
$key . ' as ' . $alias => function ($query) use ($item) {
$query->where($item['key'], $item['condition'], $item['value']);
}
]);
}
} else {
$model = $model->withCount($key);
}
}
}
if (count($withSum) > 0) {
foreach ($withSum as $key => $items) {
if (is_array($items)) {
foreach ($items as $alias => $item) {
$model = $model->withSum([
$key . ' as ' . $alias => function ($query) use ($item) {
$query->where($item['key'], $item['condition'], $item['value']);
}
], $item['sum']);
}
} else {
$model = $model->withSum($key);
}
}
}
return $model->find($id);
}
}