Skip to content

Commit 7a533c4

Browse files
author
Marius
committed
Add withRelations, withRelationsCount and withRelationsExistence to list
1 parent 304101c commit 7a533c4

File tree

1 file changed

+25
-31
lines changed

1 file changed

+25
-31
lines changed

src/Services/BaseResourceService.php

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -86,41 +86,35 @@ public function list(array $request): Builder
8686
foreach ($filters as $column => $value) {
8787
if (!\is_array($value)) {
8888
$builder->where($column, '=', $value);
89-
90-
continue;
91-
}
92-
93-
if ($column === 'withRelations') {
94-
$builder->with($this->getValidRelations($value));
95-
96-
continue;
9789
}
90+
}
9891

99-
if ($column === 'withRelationsCount') {
100-
$validRelations = $this->getValidRelations($value);
101-
$builder->withCount(\array_map(
102-
fn (string $relationName): string => $relationName . ' as ' . $relationName . static::COUNT_ALIAS_POSTFIX,
103-
$validRelations
104-
));
105-
$possibleSortColumns = \array_merge($possibleSortColumns, \array_map(
106-
fn (string $relationName): string => $relationName . static::COUNT_ALIAS_POSTFIX,
107-
$validRelations
108-
));
92+
if (\is_array($request['withRelations'] ?? null)) {
93+
$builder->with($this->getValidRelations($request['withRelations']));
94+
}
10995

110-
continue;
111-
}
96+
if (\is_array($request['withRelationsCount'] ?? null)) {
97+
$validRelations = $this->getValidRelations($request['withRelationsCount']);
98+
$builder->withCount(\array_map(
99+
fn (string $relationName): string => $relationName . ' as ' . $relationName . static::COUNT_ALIAS_POSTFIX,
100+
$validRelations
101+
));
102+
$possibleSortColumns = \array_merge($possibleSortColumns, \array_map(
103+
fn (string $relationName): string => $relationName . static::COUNT_ALIAS_POSTFIX,
104+
$validRelations
105+
));
106+
}
112107

113-
if ($column === 'withRelationsExistence') {
114-
$validRelations = $this->getValidRelations($value);
115-
$builder->withExists(\array_map(
116-
fn (string $relationName): string => $relationName . ' as ' . $relationName . static::EXIST_ALIAS_POSTFIX,
117-
$validRelations
118-
));
119-
$possibleSortColumns = \array_merge($possibleSortColumns, \array_map(
120-
fn (string $relationName): string => $relationName . static::EXIST_ALIAS_POSTFIX,
121-
$validRelations
122-
));
123-
}
108+
if (\is_array($request['withRelationsExistence'] ?? null)) {
109+
$validRelations = $this->getValidRelations($request['withRelationsExistence']);
110+
$builder->withExists(\array_map(
111+
fn (string $relationName): string => $relationName . ' as ' . $relationName . static::EXIST_ALIAS_POSTFIX,
112+
$validRelations
113+
));
114+
$possibleSortColumns = \array_merge($possibleSortColumns, \array_map(
115+
fn (string $relationName): string => $relationName . static::EXIST_ALIAS_POSTFIX,
116+
$validRelations
117+
));
124118
}
125119

126120
if ($this->shouldIgnoreSort($possibleSortColumns)) {

0 commit comments

Comments
 (0)