Skip to content

Commit 2c7afca

Browse files
author
user
committed
fix exception in collection if model use soft delete
1 parent 1755360 commit 2c7afca

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

src/PostgresEngine.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,10 @@ protected function performSearch(Builder $builder, $perPage = 0, $page = 1)
217217
->whereRaw("$indexColumn @@ query")
218218
->orderBy('rank', 'desc')
219219
->orderBy($builder->model->getKeyName());
220-
220+
//if model use soft delete - without trashed
221+
if(method_exists($builder->model, 'getDeletedAtColumn')){
222+
$query->where($builder->model->getDeletedAtColumn(),null);
223+
}
221224
if ($perPage > 0) {
222225
$query->skip(($page - 1) * $perPage)
223226
->limit($perPage);

tests/PostgresEngineTest.php

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function test_update_adds_object_to_index()
3737
$table->shouldReceive('where')
3838
->with('id', '=', 1)
3939
->andReturnSelf();
40+
4041
$table->shouldReceive('update')
4142
->with(['searchable' => 'foo']);
4243

@@ -97,6 +98,24 @@ public function test_search()
9798

9899
$engine->search($builder);
99100
}
101+
102+
public function test_search_with_soft_delete()
103+
{
104+
list($engine, $db) = $this->getEngine();
105+
106+
$table = $this->setDbExpectations($db);
107+
108+
$table->shouldReceive('skip')->with(0)->andReturnSelf()
109+
->shouldReceive('limit')->with(5)->andReturnSelf()
110+
->shouldReceive('where')->with('bar', 1)->andReturnSelf()
111+
->shouldReceive('where')->with('deleted_at', null);
112+
$db->shouldReceive('select')->with(null, ['foo', 1]);
113+
114+
$builder = new Builder(new TestWithSoftDeleteModel(), 'foo');
115+
$builder->where('bar', 1)->take(5);
116+
117+
$engine->search($builder);
118+
}
100119

101120
public function test_map_correctly_maps_results_to_models()
102121
{
@@ -168,7 +187,7 @@ protected function setDbExpectations($db, $skip = 0, $limit = 5)
168187
->shouldReceive('orderBy')->with('rank', 'desc')->andReturnSelf()
169188
->shouldReceive('orderBy')->with('id')->andReturnSelf()
170189
->shouldReceive('toSql');
171-
190+
172191
return $table;
173192
}
174193
}
@@ -227,3 +246,60 @@ public function searchableAdditionalArray()
227246
return $this->searchableAdditionalArray;
228247
}
229248
}
249+
250+
class TestWithSoftDeleteModel extends Model
251+
{
252+
use \Illuminate\Database\Eloquent\SoftDeletes;
253+
254+
public $id = 1;
255+
256+
public $text = 'Foo';
257+
258+
protected $searchableOptions = [
259+
'rank' => [
260+
'fields' => [
261+
'nullable' => 'B',
262+
],
263+
],
264+
];
265+
266+
protected $searchableAdditionalArray = [];
267+
268+
public function searchableAs()
269+
{
270+
return 'searchable';
271+
}
272+
273+
public function getKeyName()
274+
{
275+
return 'id';
276+
}
277+
278+
public function getKey()
279+
{
280+
return $this->id;
281+
}
282+
283+
public function getTable()
284+
{
285+
return 'table';
286+
}
287+
288+
public function toSearchableArray()
289+
{
290+
return [
291+
'text' => $this->text,
292+
'nullable' => null,
293+
];
294+
}
295+
296+
public function searchableOptions()
297+
{
298+
return $this->searchableOptions;
299+
}
300+
301+
public function searchableAdditionalArray()
302+
{
303+
return $this->searchableAdditionalArray;
304+
}
305+
}

0 commit comments

Comments
 (0)