This commit is contained in:
pbao9
2026-02-02 10:06:42 +07:00
parent a2a75b2015
commit 34575b2d96
29 changed files with 253 additions and 157 deletions
+24 -11
View File
@@ -15,19 +15,32 @@ class OsCachedRepository extends BaseCachedRepository implements OsRepositoryInt
{
return $this->osRepository->getModel();
}
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
{
public function filter(
array $data = [],
array $relationships = [],
array $withCount = [],
array $select = ['*'],
bool $returnQuery = false
) {
if (
$returnQuery ||
!empty($data['paginate'])
) {
return $this->osRepository
->filter($data, $relationships, $withCount, $select, $returnQuery);
}
$cacheKey = $this->getCacheKey(md5(json_encode([
'filterItem' => [
'data' => $data,
'relationships' => $relationships,
'select' => $select,
'withCount' => $withCount
]
'filter' => compact('data', 'relationships', 'withCount', 'select')
])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($data, $relationships, $select, $withCount) {
return $this->osRepository->filter($data, $relationships, $withCount, $select);
});
return Cache::remember(
$cacheKey,
$this->cacheTime,
fn() =>
$this->osRepository
->filter($data, $relationships, $withCount, $select, $returnQuery)
);
}
public function findWithRelationships(int $id = 0, array $relationships = [],array $withCount = [], array $withSum = [])
{
+26 -24
View File
@@ -11,34 +11,36 @@ class OsRepository extends BaseRepository implements OsRepositoryInterface
{
return Os::class;
}
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
{
$model = $this->model->with($relationships)->select($select);
// Apply filters from $data
if (isset($data['os_id'])) {
$model = $model->where('os_group_id', $data['os_id']);
public function filter(
array $data = [],
array $relationships = [],
array $withCount = [],
array $select = ['*'],
bool $returnQuery = false
) {
$query = $this->model
->select($select)
->with($relationships);
if (!empty($data['module_host_id'])) {
$query->where('module_host_id', $data['module_host_id']);
}
if (isset($data['os_group_id'])) {
$model = $model->where('os_group_id', $data['os_group_id']);
if (!empty($withCount)) {
$query->withCount($withCount);
}
if (isset($data['module_host_id'])) {
$model = $model->where('module_host_id', $data['module_host_id']);
if ($returnQuery) {
return $query;
}
if (isset($data['status'])) {
$model = $model->where('status', $data['status']);
if (!empty($data['paginate'])) {
return $query->paginate(
$data['per_page'] ?? 15
);
}
if (count($withCount) > 0) {
$model = $model->withCount($withCount);
}
if(isset($data['page'])) {
return $model->paginate(15);
}
return $model->get();
return $query->get();
}
public function findWithRelationships(int $id = 0, array $relationships = [], array $withCount = [], array $withSum = [])
{
@@ -5,7 +5,13 @@ namespace App\Repositories\Os;
use App\Repositories\BaseRepositoryInterface;
interface OsRepositoryInterface extends BaseRepositoryInterface {
public function filter(array $data = [],array $relationships = [],array $withCount = [],array $select = ['*']);
public function filter(
array $data = [],
array $relationships = [],
array $withCount = [],
array $select = ['*'],
bool $returnQuery = false
);
public function findWithRelationships(int $id = 0, array $relationships = [],array $withCount = [], array $withSum = []);
}