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

Query in getModelsPerType() only by ids #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions src/ModelToSearchThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\ForwardsCalls;

class ModelToSearchThrough
{
use ForwardsCalls;

/**
* Builder to search through.
*/
Expand Down Expand Up @@ -66,6 +69,19 @@ public function __construct(Builder $builder, Collection $columns, string $order
$this->fullTextRelation = $fullTextRelation;
}

/**
* Handle dynamic method calls into the builder instance
*
* @param string $method
* @param array $parameters
* @return static
*/
public function __call($method, $parameters): static
{
$this->forwardCallTo($this->builder, $method, $parameters);
return $this;
}

/**
* Setter for the orderBy column.
*
Expand Down
2 changes: 1 addition & 1 deletion src/SearchFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __call($method, $parameters)
/**
* Returns a new Searcher instance.
*
* @return \ProtoneMedia\LaravelCrossEloquentSearch\Searcher
* @return \Konekt\Search\Searcher
*/
public function new(): Searcher
{
Expand Down
19 changes: 18 additions & 1 deletion src/Searcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\ForwardsCalls;
use Konekt\Search\Contracts\SearchDialect;
use Konekt\Search\Dialects\MySQLDialect;
use Konekt\Search\Dialects\PostgresDialect;
Expand All @@ -28,6 +29,7 @@
class Searcher
{
use Conditionable;
use ForwardsCalls;

protected Collection $modelsToSearchThrough;

Expand Down Expand Up @@ -70,6 +72,21 @@ public function __construct()
$this->orderByAsc();
}

/**
* Handle dynamic method calls into the ModelToSearchThrough instances.
*
* @param string $method
* @param array $parameters
* @return static
*/
public function __call($method, $parameters)
{
$this->modelsToSearchThrough->each(fn(ModelToSearchThrough $modelToSearchThrough) =>
$this->forwardCallTo($modelToSearchThrough, $method, $parameters)
);
return $this;
}

public function isCaseInsensitive(): bool
{
return $this->ignoreCase;
Expand Down Expand Up @@ -518,7 +535,7 @@ protected function getModelsPerType(Collection|LengthAwarePaginator|PaginatorCon
$ids = $results->pluck($key)->filter();

return $ids->isNotEmpty()
? $modelToSearchThrough->getFreshBuilder()->whereKey($ids)->get()->keyBy->getKey()
? $modelToSearchThrough->getModel()->newQueryWithoutScopes()->whereKey($ids)->get()->keyBy->getKey()
: null;
});

Expand Down
Loading