Skip to content

Commit

Permalink
allow query callback
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jul 27, 2018
1 parent fbc710f commit 9f06b9c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
17 changes: 15 additions & 2 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,19 @@ public function orderBy($column, $direction = 'asc')
return $this;
}

/**
* Set the callback that should have an opportunity to modify the database query.
*
* @param callable $callback
* @return $this
*/
public function query($callback)
{
$this->queryCallback = $queryCallback;

return $this;
}

/**
* Get the raw results of the search.
*
Expand Down Expand Up @@ -218,7 +231,7 @@ public function paginate($perPage = null, $pageName = 'page', $page = null)
$perPage = $perPage ?: $this->model->getPerPage();

$results = Collection::make($engine->map(
$rawResults = $engine->paginate($this, $perPage, $page), $this->model
$this, $rawResults = $engine->paginate($this, $perPage, $page), $this->model
));

$paginator = (new LengthAwarePaginator($results, $engine->getTotalCount($rawResults), $perPage, $page, [
Expand All @@ -245,7 +258,7 @@ public function paginateRaw($perPage = null, $pageName = 'page', $page = null)

$perPage = $perPage ?: $this->model->getPerPage();

$results = $engine->paginate($this, $perPage, $page);
$results = $engine->paginate($this, $perPage, $page);

$paginator = (new LengthAwarePaginator($results, $engine->getTotalCount($results), $perPage, $page, [
'path' => Paginator::resolveCurrentPath(),
Expand Down
5 changes: 3 additions & 2 deletions src/Engines/AlgoliaEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,19 @@ public function mapIds($results)
/**
* Map the given results to instances of the given model.
*
* @param \Laravel\Scout\Builder $builder
* @param mixed $results
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Database\Eloquent\Collection
*/
public function map($results, $model)
public function map(Builder $builder, $results, $model)
{
if (count($results['hits']) === 0) {
return Collection::make();
}

$models = $model->getScoutModelsByIds(
collect($results['hits'])->pluck('objectID')->values()->all()
$builder, collect($results['hits'])->pluck('objectID')->values()->all()
)->keyBy(function ($model) {
return $model->getScoutKey();
});
Expand Down
5 changes: 3 additions & 2 deletions src/Engines/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ abstract public function mapIds($results);
/**
* Map the given results to instances of the given model.
*
* @param \Laravel\Scout\Builder $builder
* @param mixed $results
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Database\Eloquent\Collection
*/
abstract public function map($results, $model);
abstract public function map(Builder $builder, $results, $model);

This comment has been minimized.

Copy link
@jhm-ciberman

jhm-ciberman Aug 2, 2018

Hey! I think this is a BREAKING change. This broke https://github.com/teamtnt/laravel-scout-tntsearch-driver in fact.
(issue: teamtnt/laravel-scout-tntsearch-driver#175)
Also, this should be documented on https://laravel.com/docs/master/scout

This comment has been minimized.

Copy link
@jhm-ciberman

jhm-ciberman Aug 2, 2018

Oh.. I didn't realize this was versioned fine. Sorry, my bad. I will downgrade to scout 4 and wait to TNT Search Driver to be updated.


/**
* Get the total count from a raw result returned by the engine.
Expand Down Expand Up @@ -86,7 +87,7 @@ public function keys(Builder $builder)
public function get(Builder $builder)
{
return Collection::make($this->map(
$this->search($builder), $builder->model
$builder, $this->search($builder), $builder->model
));
}
}
14 changes: 10 additions & 4 deletions src/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laravel\Scout;

use Laravel\Scout\Builder;
use Laravel\Scout\Jobs\MakeSearchable;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\SoftDeletes;
Expand Down Expand Up @@ -165,15 +166,20 @@ public function unsearchable()
/**
* Get the requested models from an array of object IDs;
*
* @param \Laravel\Scout\Builder $builder
* @param array $ids
* @return mixed
*/
public function getScoutModelsByIds(array $ids)
public function getScoutModelsByIds(Builder $builder, array $ids)
{
$builder = in_array(SoftDeletes::class, class_uses_recursive($this))
? $this->withTrashed() : $this->newQuery();
$query = in_array(SoftDeletes::class, class_uses_recursive($this))
? $this->withTrashed() : $this->newQuery();

return $builder->whereIn(
if ($builder->queryCallback) {
call_user_func($builder->queryCallback, $query);
}

return $query->whereIn(
$this->getScoutKeyName(), $ids
)->get();
}
Expand Down
4 changes: 3 additions & 1 deletion tests/AlgoliaEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public function test_map_correctly_maps_results_to_models()
$model->shouldReceive('getKeyName')->andReturn('id');
$model->shouldReceive('getScoutModelsByIds')->andReturn(Collection::make([new AlgoliaEngineTestModel]));

$results = $engine->map(['nbHits' => 1, 'hits' => [
$builder = Mockery::mock(Builder::class);

$results = $engine->map($builder, ['nbHits' => 1, 'hits' => [
['objectID' => 1, 'id' => 1],
]], $model);

Expand Down

0 comments on commit 9f06b9c

Please sign in to comment.