update 1.0.8.0
Commits for version update
This commit is contained in:
16
vendor/nicolaslopezj/searchable/README.md
vendored
16
vendor/nicolaslopezj/searchable/README.md
vendored
@@ -103,6 +103,22 @@ $users = User::where('status', 'active')
|
||||
|
||||
The above, will return all users in order of relevance.
|
||||
|
||||
## Entire Text search
|
||||
|
||||
By default, multi-word search terms are split and Searchable searches for each word individually. Relevance plays a role in prioritizing matches that matched on multiple words. If you want to prioritize matches that include the multi-word search (thus, without splitting into words) you can enable full text search by setting the third value to true. Example:
|
||||
|
||||
```php
|
||||
// Prioritize matches containing "John Doe" above matches containing only "John" or "Doe".
|
||||
$users = User::search("John Doe", null, true)->get();
|
||||
```
|
||||
|
||||
If you explicitly want to search for full text matches only, you can disable multi-word splitting by setting the fourth parameter to true.
|
||||
|
||||
```php
|
||||
// Do not include matches that only matched "John" OR "Doe".
|
||||
$users = User::search("John Doe", null, true, true)->get();
|
||||
```
|
||||
|
||||
# How does it work?
|
||||
|
||||
Searchable builds a query that search through your model using Laravel's Eloquent.
|
||||
|
@@ -28,14 +28,15 @@ trait SearchableTrait
|
||||
* @param string $search
|
||||
* @param float|null $threshold
|
||||
* @param boolean $entireText
|
||||
* @param boolean $entireTextOnly
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeSearch(Builder $q, $search, $threshold = null, $entireText = false)
|
||||
public function scopeSearch(Builder $q, $search, $threshold = null, $entireText = false, $entireTextOnly = false)
|
||||
{
|
||||
return $this->scopeSearchRestricted($q, $search, null, $threshold, $entireText);
|
||||
return $this->scopeSearchRestricted($q, $search, null, $threshold, $entireText, $entireTextOnly);
|
||||
}
|
||||
|
||||
public function scopeSearchRestricted(Builder $q, $search, $restriction, $threshold = null, $entireText = false)
|
||||
public function scopeSearchRestricted(Builder $q, $search, $restriction, $threshold = null, $entireText = false, $entireTextOnly = false)
|
||||
{
|
||||
$query = clone $q;
|
||||
$query->select($this->getTable() . '.*');
|
||||
@@ -56,9 +57,14 @@ trait SearchableTrait
|
||||
foreach ($this->getColumns() as $column => $relevance)
|
||||
{
|
||||
$relevance_count += $relevance;
|
||||
$queries = $this->getSearchQueriesForColumn($query, $column, $relevance, $words);
|
||||
|
||||
if ( $entireText === true && count($words) > 1 )
|
||||
if (!$entireTextOnly) {
|
||||
$queries = $this->getSearchQueriesForColumn($query, $column, $relevance, $words);
|
||||
} else {
|
||||
$queries = [];
|
||||
}
|
||||
|
||||
if ( ($entireText === true && count($words) > 1) || $entireTextOnly === true )
|
||||
{
|
||||
$queries[] = $this->getSearchQuery($query, $column, $relevance, [$search], 50, '', '');
|
||||
$queries[] = $this->getSearchQuery($query, $column, $relevance, [$search], 30, '%', '%');
|
||||
@@ -110,7 +116,13 @@ trait SearchableTrait
|
||||
protected function getColumns()
|
||||
{
|
||||
if (array_key_exists('columns', $this->searchable)) {
|
||||
return $this->searchable['columns'];
|
||||
$driver = $this->getDatabaseDriver();
|
||||
$prefix = Config::get("database.connections.$driver.prefix");
|
||||
$columns = [];
|
||||
foreach($this->searchable['columns'] as $column => $priority){
|
||||
$columns[$prefix . $column] = $priority;
|
||||
}
|
||||
return $columns;
|
||||
} else {
|
||||
return DB::connection()->getSchemaBuilder()->getColumnListing($this->table);
|
||||
}
|
||||
@@ -320,7 +332,7 @@ trait SearchableTrait
|
||||
*/
|
||||
protected function mergeQueries(Builder $clone, Builder $original) {
|
||||
$tableName = DB::connection($this->connection)->getTablePrefix() . $this->getTable();
|
||||
if ($this->getDatabaseDriver() == 'pgsql') {
|
||||
if ($this->getDatabaseDriver() == 'pgsql') {
|
||||
$original->from(DB::connection($this->connection)->raw("({$clone->toSql()}) as {$tableName}"));
|
||||
} else {
|
||||
$original->from(DB::connection($this->connection)->raw("({$clone->toSql()}) as `{$tableName}`"));
|
||||
|
Reference in New Issue
Block a user