laravel-scout-mysql-driver icon indicating copy to clipboard operation
laravel-scout-mysql-driver copied to clipboard

Include softDeletes in search results

Open rico opened this issue 7 years ago • 3 comments

Is there a way to include soft deleted models in the search results?

I tried several approached found around the net with no luck.

Maybe this is related to #17?

rico avatar Aug 29 '17 07:08 rico

i modified this engine a little to do the trick added these lines in src/Engines/MySQLEngine.php

// line 59
if ($builder->callback) {
    $query = call_user_func($builder->callback, $query);
}

and then i could do this in my controller

$products = Product::search($request->keyword, function($query) {
    return $query->active()->withTrashed(); // local scope and soft deletes
})->paginate(20);

sembrex avatar Sep 19 '17 23:09 sembrex

@sembrex - will this package already respect global scopes as it's querying the relevant table?

chrispage1 avatar Sep 05 '18 06:09 chrispage1

This is what I've had to use to get soft deletes in my results:

MyModel::search($text, function ($query) {
    return $query->onlyTrashed();
})->get();

Weirdly, only works with the 'soft_delete' boolean set to 'false' in app/scout.php

Taelkir avatar Sep 28 '21 13:09 Taelkir