Skip to content

Commit 6bc7bfa

Browse files
ngaspariStyleCIBot
andauthored
Searchables with relation (#59)
* Update SearchParameter.php fix relation search * Apply fixes from StyleCI --------- Co-authored-by: StyleCI Bot <[email protected]>
1 parent 393d83f commit 6bc7bfa

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/RequestParameters/SearchParameter.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class SearchParameter extends AbstractParameter
2424
const LARAVEL_WHERE = 'where';
2525
const LARAVEL_OR_WHERE = 'orWhere';
2626

27+
const RELATION_SEPARATOR = '.';
28+
2729
protected OperatorsConfig $operatorsConfig;
2830

2931
public static function getParameterName(): string
@@ -89,6 +91,17 @@ protected function makeQuery(Builder $builder, array $arguments, string $boolOpe
8991
continue;
9092
}
9193

94+
if ($this->isRelationSearch($key)) {
95+
// relation search
96+
[$rel, $attr] = explode(self::RELATION_SEPARATOR, $key, 2);
97+
98+
$builder->whereHas(Str::camel($rel), function ($query) use ($attr, $value, $functionName) {
99+
$this->makeSingleQuery($functionName, $query, $attr, $value);
100+
});
101+
102+
continue;
103+
}
104+
92105
$this->makeSingleQuery($functionName, $builder, $key, $value);
93106
}
94107
}
@@ -132,6 +145,11 @@ protected function hasSubSearch($key, $value): bool
132145
return is_string($key) && is_array($value);
133146
}
134147

148+
protected function isRelationSearch($key): bool
149+
{
150+
return str_contains($key, self::RELATION_SEPARATOR);
151+
}
152+
135153
/**
136154
* @param string $functionName
137155
* @param Builder $builder

0 commit comments

Comments
 (0)