@@ -216,8 +216,6 @@ protected function performSearch(Builder $builder, $perPage = 0, $page = 1)
216216 // does not revceive a model instance
217217 $ this ->preserveModel ($ builder ->model );
218218
219- $ indexColumn = $ this ->getIndexColumn ($ builder ->model );
220-
221219 $ bindings = collect ([]);
222220
223221 // 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)
228226 $ bindings ->push ($ this ->searchConfig ($ builder ->model ) ?: null )
229227 ->push ($ builder ->query );
230228
229+ $ indexColumn = $ this ->getIndexColumn ($ builder ->model );
230+
231231 // Build the query
232232 $ query = $ this ->database
233233 ->table ($ builder ->index ?: $ builder ->model ->searchableAs ())
@@ -238,21 +238,26 @@ protected function performSearch(Builder $builder, $perPage = 0, $page = 1)
238238 ->whereRaw ("$ indexColumn @@ query " )
239239 ->orderBy ('rank ' , 'desc ' )
240240 ->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- }
249241
250242 // Transfer the where clauses that were set on the builder instance if any
251243 foreach ($ builder ->wheres as $ key => $ value ) {
252244 $ query ->where ($ key , $ value );
253245 $ bindings ->push ($ value );
254246 }
255247
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+
256261 return $ this ->database
257262 ->select ($ query ->toSql (), $ bindings ->all ());
258263 }
@@ -277,7 +282,7 @@ public function mapIds($results)
277282 *
278283 * @param mixed $results
279284 * @param \Illuminate\Database\Eloquent\Model $model
280- * @return Collection
285+ * @return \Illuminate\Support\ Collection
281286 */
282287 public function map ($ results , $ model )
283288 {
@@ -293,9 +298,11 @@ public function map($results, $model)
293298 ->get ()
294299 ->keyBy ($ model ->getKeyName ());
295300
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+ });
299306 }
300307
301308 /**
@@ -490,4 +497,15 @@ protected function searchConfig(Model $model)
490497 {
491498 return $ this ->option ($ model , 'config ' , $ this ->config ('config ' , '' ));
492499 }
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+ }
493511}
0 commit comments