diff --git a/README.md b/README.md index a2831fa..f268ab8 100755 --- a/README.md +++ b/README.md @@ -106,6 +106,15 @@ You can also override these methods using the following syntax when running a se Searchy::driver('fuzzy')->users('name')->query('Batman')->get(); ``` + +## Soft Deletes + +By default soft deletes will not be included in your results. However, if you wish to include soft deletes you can add do so by followin the below syntax. + +```php +Searchy::trashed()->users('name')->query('Batman')->get(); +``` + ## Drivers Searchy takes advantage of 'Drivers' to handle matching various conditions of the fields you specify. diff --git a/composer.json b/composer.json index 305dba9..933b909 100755 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "tom-lingham/searchy", + "name": "jakeboyles/laravel-searchy", "description": "Laravel Searchy makes user driven searching easy with fuzzy search, basic string matching, Levenshtein Distance and more.", "keywords": ["laravel","search","fuzzy"], "license": "MIT", @@ -18,7 +18,7 @@ }, "autoload": { "psr-4": { - "TomLingham\\Searchy\\": "src/" + "jakeboyles\\Searchy\\": "src/" } }, "config": { diff --git a/src/SearchBuilder.php b/src/SearchBuilder.php index 217c3b9..258fb81 100755 --- a/src/SearchBuilder.php +++ b/src/SearchBuilder.php @@ -28,6 +28,11 @@ class SearchBuilder /** * @var */ + private $trashed = false; + + /** + * @var + */ private $config; public function __construct(Repository $config) @@ -73,6 +78,19 @@ class SearchBuilder return $this; } + + /** + * @param $trashed + * + * @return $this + */ + public function trashed() + { + $this->trashed = true; + + return $this; + } + /** * @param $table * @param $searchFields @@ -104,7 +122,7 @@ class SearchBuilder // Create a new instance of the selected drivers 'class' and pass // through table and fields to search - $driverInstance = new $driver($this->table, $this->searchFields, $relevanceFieldName); + $driverInstance = new $driver($this->table, $this->searchFields, $relevanceFieldName,['*'],$this->trashed); return $driverInstance; } diff --git a/src/SearchDrivers/BaseSearchDriver.php b/src/SearchDrivers/BaseSearchDriver.php index 2c96b75..719fa74 100755 --- a/src/SearchDrivers/BaseSearchDriver.php +++ b/src/SearchDrivers/BaseSearchDriver.php @@ -18,6 +18,8 @@ abstract class BaseSearchDriver implements SearchDriverInterface protected $query; + protected $trashed; + /** * @param null $table * @param array $searchFields @@ -26,12 +28,13 @@ abstract class BaseSearchDriver implements SearchDriverInterface * * @internal param $relevanceField */ - public function __construct($table = null, $searchFields = [], $relevanceFieldName, $columns = ['*']) + public function __construct($table = null, $searchFields = [], $relevanceFieldName, $columns = ['*'],$trashed) { $this->searchFields = $searchFields; $this->table = $table; $this->columns = $columns; $this->relevanceFieldName = $relevanceFieldName; + $this->trashed = $trashed; } /** @@ -97,11 +100,24 @@ abstract class BaseSearchDriver implements SearchDriverInterface */ protected function run() { - $this->query = \DB::table($this->table) - ->select($this->columns) - ->addSelect($this->buildSelectQuery($this->searchFields)) - ->orderBy($this->relevanceFieldName, 'desc') - ->having($this->relevanceFieldName, '>', 0); + // If they included trashed flag then give them all records including soft deletes + if($this->trashed) + { + $this->query = \DB::table($this->table) + ->select($this->columns) + ->addSelect($this->buildSelectQuery($this->searchFields)) + ->orderBy($this->relevanceFieldName, 'desc') + ->having($this->relevanceFieldName, '>', 0); + } + else + { + $this->query = \DB::table($this->table) + ->select($this->columns) + ->where('deleted_at',NULL) + ->addSelect($this->buildSelectQuery($this->searchFields)) + ->orderBy($this->relevanceFieldName, 'desc') + ->having($this->relevanceFieldName, '>', 0); + } return $this->query; }