Adding 'withTrashed()' method to include soft deleted records and also removed soft deleted records by default. Fixed #15.
This commit is contained in:
17
README.md
17
README.md
@ -82,6 +82,14 @@ Sometimes you may want to leverage searches on concatenated column. For example,
|
||||
$users = Searchy::users('first_name::last_name')->query('John Smith')->get();
|
||||
```
|
||||
|
||||
### Soft Deleted Records
|
||||
|
||||
By default soft deletes will not be included in your results. However, if you wish to include soft deleted records you can do so by adding the `withTrashed()` after specifying your table and fields;
|
||||
|
||||
```php
|
||||
Searchy::users('name')->withTrashed()->query('Batman')->get();
|
||||
```
|
||||
|
||||
### Return only specific columns
|
||||
You can specify which columns to return in your search:
|
||||
|
||||
@ -106,15 +114,6 @@ 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.
|
||||
|
@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "jakeboyles/laravel-searchy",
|
||||
"name": "tom-lingham/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": {
|
||||
"jakeboyles\\Searchy\\": "src/"
|
||||
"TomLingham\\Searchy\\": "src/"
|
||||
}
|
||||
},
|
||||
"config": {
|
||||
|
@ -28,11 +28,6 @@ class SearchBuilder
|
||||
/**
|
||||
* @var
|
||||
*/
|
||||
private $trashed = false;
|
||||
|
||||
/**
|
||||
* @var
|
||||
*/
|
||||
private $config;
|
||||
|
||||
public function __construct(Repository $config)
|
||||
@ -78,19 +73,6 @@ class SearchBuilder
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $trashed
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function trashed()
|
||||
{
|
||||
$this->trashed = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $table
|
||||
* @param $searchFields
|
||||
@ -122,7 +104,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,['*'],$this->trashed);
|
||||
$driverInstance = new $driver($this->table, $this->searchFields, $relevanceFieldName, ['*']);
|
||||
|
||||
return $driverInstance;
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace TomLingham\Searchy\SearchDrivers;
|
||||
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use TomLingham\Searchy\Interfaces\SearchDriverInterface;
|
||||
|
||||
abstract class BaseSearchDriver implements SearchDriverInterface
|
||||
@ -18,7 +19,7 @@ abstract class BaseSearchDriver implements SearchDriverInterface
|
||||
|
||||
protected $query;
|
||||
|
||||
protected $trashed;
|
||||
protected $withTrashed;
|
||||
|
||||
/**
|
||||
* @param null $table
|
||||
@ -28,15 +29,28 @@ abstract class BaseSearchDriver implements SearchDriverInterface
|
||||
*
|
||||
* @internal param $relevanceField
|
||||
*/
|
||||
public function __construct($table = null, $searchFields = [], $relevanceFieldName, $columns = ['*'],$trashed)
|
||||
public function __construct($table = null, $searchFields = [], $relevanceFieldName, $columns = ['*'])
|
||||
{
|
||||
$this->searchFields = $searchFields;
|
||||
$this->table = $table;
|
||||
$this->columns = $columns;
|
||||
$this->relevanceFieldName = $relevanceFieldName;
|
||||
$this->trashed = $trashed;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Specify whether to return soft deleted items or not
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function withTrashed()
|
||||
{
|
||||
$this->withTrashed = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Specify which columns to return.
|
||||
*
|
||||
@ -100,26 +114,18 @@ abstract class BaseSearchDriver implements SearchDriverInterface
|
||||
*/
|
||||
protected function run()
|
||||
{
|
||||
// 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);
|
||||
}
|
||||
$this->query = \DB::table($this->table)
|
||||
->select($this->columns)
|
||||
->addSelect($this->buildSelectQuery($this->searchFields));
|
||||
|
||||
return $this->query;
|
||||
// If they included withTrashed flag then give them all records including soft deletes
|
||||
// Check to ensure the column exists before committing
|
||||
if( ! $this->withTrashed && in_array('deleted_at', Schema::getColumnListing($this->table)) )
|
||||
$this->query = $this->query->where('deleted_at', NULL);
|
||||
|
||||
return $this->query
|
||||
->orderBy($this->relevanceFieldName, 'desc')
|
||||
->having($this->relevanceFieldName, '>', 0);;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user