Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Searchables with relation #59

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/RequestParameters/SearchParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class SearchParameter extends AbstractParameter
const LARAVEL_WHERE = 'where';
const LARAVEL_OR_WHERE = 'orWhere';

const RELATION_SEPARATOR = '.';

protected OperatorsConfig $operatorsConfig;

public static function getParameterName(): string
Expand Down Expand Up @@ -89,6 +91,17 @@ protected function makeQuery(Builder $builder, array $arguments, string $boolOpe
continue;
}

if ($this->isRelationSearch($key)) {
// relation search
[$rel, $attr] = explode(self::RELATION_SEPARATOR, $key, 2);

$builder->whereHas(Str::camel($rel), function ($query) use ($attr, $value, $functionName) {
$this->makeSingleQuery($functionName, $query, $attr, $value);
});

continue;
}

$this->makeSingleQuery($functionName, $builder, $key, $value);
}
}
Expand Down Expand Up @@ -132,6 +145,11 @@ protected function hasSubSearch($key, $value): bool
return is_string($key) && is_array($value);
}

protected function isRelationSearch($key): bool
{
return str_contains($key, self::RELATION_SEPARATOR);
}

/**
* @param string $functionName
* @param Builder $builder
Expand Down
Loading