Search Column querying more than where() #5671
-
Hi, I recently implemented my own version of /**
* Apply the search.
*
* @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation $query
* @param string $search
* @return \Illuminate\Database\Eloquent\Builder
*/
public function __invoke($query, $search, string $connectionType, string $whereOperator = 'orWhere')
{
if (in_array($connectionType, ['mysql', 'pgsql'])) {
$query
->selectRaw('*, MATCH('.implode(', ', $this->columnName($query)).') AGAINST (? IN NATURAL LANGUAGE MODE) as _search_relevance', [$search])
->{$whereOperator.'FullText'}(
$this->columnName($query), $search
)
->orderBy('_search_relevance', 'desc');
}
return $query;
} The issue is - the Nova /**
* Get the raw results of the search.
*
* @param class-string<\Laravel\Nova\Resource> $resourceClass
* @param array<int, string|\Laravel\Nova\Query\Search\Column> $searchColumns
* @return mixed
*/
public function handle($resourceClass, array $searchColumns)
{
return $this->queryBuilder->where(function ($query) use ($searchColumns) { // <---
$connectionType = $query->getModel()->getConnection()->getDriverName();
collect($searchColumns)
->each(function ($column) use ($query, $connectionType) {
if ($column instanceof Column || (! is_string($column) && is_callable($column))) {
$column($query, $this->searchKeyword, $connectionType);
} else {
Column::from($column)->__invoke($query, $this->searchKeyword, $connectionType);
}
});
});
} Meaning the I'd like to hear if this is the right way to approach sorting search results when using a custom column, and if it is - if perhaps the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
This seems more suitable as Laravel Scout driver unless you want to make a replacement to |
Beta Was this translation helpful? Give feedback.
This seems more suitable as Laravel Scout driver unless you want to make a replacement to
Laravel\Nova\Query\Search
using Laravel Container.