Skip to content

Commit 06e57be

Browse files
committed
Merge branch 'handle-soft-delete' of https://github.com/MetaDone/laravel-scout-postgres into MetaDone-handle-soft-delete
2 parents a0a3e20 + 5e37a0b commit 06e57be

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/PostgresEngine.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,10 @@ protected function performSearch(Builder $builder, $perPage = 0, $page = 1)
238238
->whereRaw("$indexColumn @@ query")
239239
->orderBy('rank', 'desc')
240240
->orderBy($builder->model->getKeyName());
241-
241+
//if model use soft delete - without trashed
242+
if (method_exists($builder->model, 'getDeletedAtColumn')) {
243+
$query->where($builder->model->getDeletedAtColumn(), null);
244+
}
242245
if ($perPage > 0) {
243246
$query->skip(($page - 1) * $perPage)
244247
->limit($perPage);

tests/PostgresEngineTest.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Database\Eloquent\Model;
99
use ScoutEngines\Postgres\PostgresEngine;
1010
use Illuminate\Database\Eloquent\Collection;
11+
use Illuminate\Database\Eloquent\SoftDeletes;
1112
use Illuminate\Database\ConnectionResolverInterface;
1213

1314
class PostgresEngineTest extends AbstractTestCase
@@ -40,6 +41,7 @@ public function test_update_adds_object_to_index()
4041
$table->shouldReceive('where')
4142
->with('id', '=', 1)
4243
->andReturnSelf();
44+
4345
$table->shouldReceive('update')
4446
->with(['searchable' => 'foo']);
4547

@@ -152,6 +154,25 @@ public function test_search_with_model_config()
152154
$engine->search($builder);
153155
}
154156

157+
public function test_search_with_soft_delete()
158+
{
159+
list($engine, $db) = $this->getEngine();
160+
161+
$table = $this->setDbExpectations($db);
162+
163+
$table->shouldReceive('skip')->with(0)->andReturnSelf()
164+
->shouldReceive('limit')->with(5)->andReturnSelf()
165+
->shouldReceive('where')->with('bar', 1)->andReturnSelf()
166+
->shouldReceive('where')->with('deleted_at', null);
167+
168+
$db->shouldReceive('select')->with(null, [null, 'foo', 1]);
169+
170+
$builder = new Builder(new SoftDeletableTestModel(), 'foo');
171+
$builder->where('bar', 1)->take(5);
172+
173+
$engine->search($builder);
174+
}
175+
155176
public function test_map_correctly_maps_results_to_models()
156177
{
157178
list($engine) = $this->getEngine();
@@ -244,8 +265,6 @@ class TestModel extends Model
244265
{
245266
public $id = 1;
246267

247-
// public $text = 'Foo';
248-
249268
public $searchableOptions = [
250269
'rank' => [
251270
'fields' => [
@@ -296,3 +315,8 @@ public function searchableAdditionalArray()
296315
return $this->searchableAdditionalArray;
297316
}
298317
}
318+
319+
class SoftDeletableTestModel extends TestModel
320+
{
321+
use SoftDeletes;
322+
}

0 commit comments

Comments
 (0)