@@ -216,8 +216,6 @@ protected function performSearch(Builder $builder, $perPage = 0, $page = 1)
216
216
// does not revceive a model instance
217
217
$ this ->preserveModel ($ builder ->model );
218
218
219
- $ indexColumn = $ this ->getIndexColumn ($ builder ->model );
220
-
221
219
$ bindings = collect ([]);
222
220
223
221
// The choices of parser, dictionaries and which types of tokens to index are determined
@@ -228,6 +226,8 @@ protected function performSearch(Builder $builder, $perPage = 0, $page = 1)
228
226
$ bindings ->push ($ this ->searchConfig ($ builder ->model ) ?: null )
229
227
->push ($ builder ->query );
230
228
229
+ $ indexColumn = $ this ->getIndexColumn ($ builder ->model );
230
+
231
231
// Build the query
232
232
$ query = $ this ->database
233
233
->table ($ builder ->index ?: $ builder ->model ->searchableAs ())
@@ -238,21 +238,26 @@ protected function performSearch(Builder $builder, $perPage = 0, $page = 1)
238
238
->whereRaw ("$ indexColumn @@ query " )
239
239
->orderBy ('rank ' , 'desc ' )
240
240
->orderBy ($ builder ->model ->getKeyName ());
241
- //if model use soft delete - without trashed
242
- if (method_exists ($ builder ->model , 'getDeletedAtColumn ' )) {
243
- $ query ->where ($ builder ->model ->getDeletedAtColumn (), null );
244
- }
245
- if ($ perPage > 0 ) {
246
- $ query ->skip (($ page - 1 ) * $ perPage )
247
- ->limit ($ perPage );
248
- }
249
241
250
242
// Transfer the where clauses that were set on the builder instance if any
251
243
foreach ($ builder ->wheres as $ key => $ value ) {
252
244
$ query ->where ($ key , $ value );
253
245
$ bindings ->push ($ value );
254
246
}
255
247
248
+ // If parsed documents are being stored in the model's table
249
+ if (! $ this ->isExternalIndex ($ builder ->model )) {
250
+ // and the model uses soft deletes we need to exclude trashed rows
251
+ if ($ this ->usesSoftDeletes ($ builder ->model )) {
252
+ $ query ->whereNull ($ builder ->model ->getDeletedAtColumn ());
253
+ }
254
+ }
255
+
256
+ if ($ perPage > 0 ) {
257
+ $ query ->skip (($ page - 1 ) * $ perPage )
258
+ ->limit ($ perPage );
259
+ }
260
+
256
261
return $ this ->database
257
262
->select ($ query ->toSql (), $ bindings ->all ());
258
263
}
@@ -277,7 +282,7 @@ public function mapIds($results)
277
282
*
278
283
* @param mixed $results
279
284
* @param \Illuminate\Database\Eloquent\Model $model
280
- * @return Collection
285
+ * @return \Illuminate\Support\ Collection
281
286
*/
282
287
public function map ($ results , $ model )
283
288
{
@@ -293,9 +298,11 @@ public function map($results, $model)
293
298
->get ()
294
299
->keyBy ($ model ->getKeyName ());
295
300
296
- return $ results ->map (function ($ result ) use ($ model , $ models ) {
297
- return $ models [$ result ->{$ model ->getKeyName ()}];
298
- });
301
+ return $ results ->pluck ($ model ->getKeyName ())
302
+ ->intersect ($ models ->keys ()) // Filter out no longer existing models (i.e. soft deleted)
303
+ ->map (function ($ key ) use ($ model , $ models ) {
304
+ return $ models [$ key ];
305
+ });
299
306
}
300
307
301
308
/**
@@ -490,4 +497,15 @@ protected function searchConfig(Model $model)
490
497
{
491
498
return $ this ->option ($ model , 'config ' , $ this ->config ('config ' , '' ));
492
499
}
500
+
501
+ /**
502
+ * Checks if the model uses the SoftDeletes trait.
503
+ *
504
+ * @param \Illuminate\Database\Eloquent\Model $model
505
+ * @return bool
506
+ */
507
+ protected function usesSoftDeletes (Model $ model )
508
+ {
509
+ return method_exists ($ model , 'getDeletedAtColumn ' );
510
+ }
493
511
}
0 commit comments