Skip to content

Commit e5a1a77

Browse files
authored
Relation update #minor (#27)
* Updated chained relations * Apply fixes from StyleCI
1 parent 5784285 commit e5a1a77

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

src/RequestParameters/SearchParameter.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Asseco\JsonQueryBuilder\SearchCallbacks\AbstractCallback;
1010
use Asseco\JsonQueryBuilder\SearchParser;
1111
use Illuminate\Database\Eloquent\Builder;
12-
use Illuminate\Support\Str;
1312

1413
class SearchParameter extends AbstractParameter
1514
{
@@ -51,8 +50,6 @@ protected function appendQuery(): void
5150
protected function makeQuery(Builder $builder, array $arguments, string $boolOperator = self:: AND): void
5251
{
5352
foreach ($arguments as $key => $value) {
54-
$key = $this->forceCamelCaseOnRelationKeys($key);
55-
5653
if ($this->isBoolOperator($key)) {
5754
// Recursion for keys which are &&/||
5855
$this->makeQuery($builder, $value, $key);
@@ -73,21 +70,6 @@ protected function makeQuery(Builder $builder, array $arguments, string $boolOpe
7370
}
7471
}
7572

76-
protected function forceCamelCaseOnRelationKeys($key)
77-
{
78-
if (!is_string($key)) {
79-
return $key;
80-
}
81-
82-
$exploded = explode('.', $key);
83-
84-
if (count($exploded) < 2) {
85-
return $key;
86-
}
87-
88-
return implode('.', [Str::camel($exploded[0]), $exploded[1]]);
89-
}
90-
9173
protected function isBoolOperator($key): bool
9274
{
9375
return in_array($key, [self:: OR, self:: AND], true);
@@ -143,6 +125,7 @@ protected function applyArguments(Builder $builder, OperatorsConfig $operatorsCo
143125
$builder->orWhere(function ($builder) use ($splitArgument, $operatorsConfig, $column) {
144126
foreach ($splitArgument as $argument) {
145127
$searchModel = new SearchParser($this->modelConfig, $operatorsConfig, $column, $argument);
128+
146129
$this->appendSingle($builder, $operatorsConfig, $searchModel);
147130
}
148131
});

src/SearchCallbacks/AbstractCallback.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Asseco\JsonQueryBuilder\Exceptions\JsonQueryBuilderException;
99
use Asseco\JsonQueryBuilder\SearchParser;
1010
use Illuminate\Database\Eloquent\Builder;
11+
use Illuminate\Support\Str;
1112

1213
abstract class AbstractCallback
1314
{
@@ -25,7 +26,6 @@ public function __construct(Builder $builder, SearchParser $searchParser)
2526
{
2627
$this->builder = $builder;
2728
$this->searchParser = $searchParser;
28-
2929
$this->categorizedValues = new CategorizedValues($this->searchParser);
3030

3131
$this->builder->when(
@@ -57,12 +57,20 @@ abstract public static function operator(): string;
5757
*/
5858
abstract public function execute(Builder $builder, string $column, CategorizedValues $values): void;
5959

60-
protected function appendRelations(Builder $builder, string $column, CategorizedValues $values): void
60+
protected function appendRelations(Builder $builder, string $column, CategorizedValues $values, string $method = 'orWhereHas'): void
6161
{
62-
[$relationName, $relatedColumn] = explode('.', $column);
62+
[$relationName, $relatedColumns] = explode('.', $column, 2);
63+
64+
$builder->{$method}(Str::camel($relationName), function (Builder $builder) use ($relatedColumns, $values) {
65+
66+
// Support for inner relation calls like model.relation.relation2.relation2_attribute
67+
if (str_contains($relatedColumns, '.')) {
68+
$this->appendRelations($builder, $relatedColumns, $values, 'whereHas');
69+
70+
return;
71+
}
6372

64-
$builder->orWhereHas($relationName, function (Builder $builder) use ($relatedColumn, $values) {
65-
$this->execute($builder, $relatedColumn, $values);
73+
$this->execute($builder, $relatedColumns, $values);
6674
});
6775
}
6876

0 commit comments

Comments
 (0)