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

Added the ability to pass an array of options to full-text search. #606

Merged
merged 3 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion src/Attributes/SearchUsingFullText.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@ class SearchUsingFullText
*/
public $columns = [];

/**
* The full-text options.
*/
public $options = [];

/**
* Create a new attribute instance.
*
* @param array|string $columns
* @return void
*/
public function __construct($columns)
public function __construct($columns, $options = [])
{
$this->columns = Arr::wrap($columns);
$this->options = Arr::wrap($options);
}
}
24 changes: 23 additions & 1 deletion src/Engines/DatabaseEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ protected function initializeSearchQuery(Builder $builder, array $columns, array
if (in_array($column, $fullTextColumns)) {
$query->orWhereFullText(
$builder->model->qualifyColumn($column),
$builder->query
$builder->query,
$this->getFullTextOptions($builder)
);
} else {
if ($canSearchPrimaryKey && $column === $builder->model->getKeyName()) {
Expand Down Expand Up @@ -280,6 +281,27 @@ protected function getAttributeColumns(Builder $builder, $attributeClass)
return $columns;
}

/**
* Get the full-text search options for the query.
*
* @param \Laravel\Scout\Builder $builder
* @return array
*/
protected function getFullTextOptions(Builder $builder)
{
$options = [];

if (PHP_MAJOR_VERSION < 8) {
return [];
}

foreach ((new ReflectionMethod($builder->model, 'toSearchableArray'))->getAttributes(SearchUsingFullText::class) as $attribute) {
$options = array_merge($options, Arr::wrap($attribute->getArguments()[1]));
}

return $options;
}

/**
* Pluck and return the primary keys of the given results.
*
Expand Down