Laravel version update
Laravel version update
This commit is contained in:
1
vendor/nicolaslopezj/searchable/.gitignore
vendored
Normal file
1
vendor/nicolaslopezj/searchable/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
.DS_Store
|
13
vendor/nicolaslopezj/searchable/README.md
vendored
13
vendor/nicolaslopezj/searchable/README.md
vendored
@@ -32,6 +32,13 @@ class User extends \Eloquent
|
||||
* @var array
|
||||
*/
|
||||
protected $searchable = [
|
||||
/**
|
||||
* Columns and their priority in search results.
|
||||
* Columns with higher values are more important.
|
||||
* Columns with equal values have equal importance.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
'columns' => [
|
||||
'users.first_name' => 10,
|
||||
'users.last_name' => 10,
|
||||
@@ -124,7 +131,7 @@ $users = User::search("John Doe", null, true, true)->get();
|
||||
Searchable builds a query that search through your model using Laravel's Eloquent.
|
||||
Here is an example query
|
||||
|
||||
####Eloquent Model:
|
||||
#### Eloquent Model:
|
||||
```php
|
||||
use Nicolaslopezj\Searchable\SearchableTrait;
|
||||
|
||||
@@ -149,12 +156,12 @@ class User extends \Eloquent
|
||||
}
|
||||
```
|
||||
|
||||
####Search:
|
||||
#### Search:
|
||||
```php
|
||||
$search = User::search('Sed neque labore', null, true)->get();
|
||||
```
|
||||
|
||||
####Result:
|
||||
#### Result:
|
||||
```sql
|
||||
select `users`.*,
|
||||
|
||||
|
@@ -1,7 +1,6 @@
|
||||
<?php namespace Nicolaslopezj\Searchable;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Query\Expression;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
@@ -42,13 +41,17 @@ trait SearchableTrait
|
||||
$query->select($this->getTable() . '.*');
|
||||
$this->makeJoins($query);
|
||||
|
||||
if ( ! $search)
|
||||
if ($search === false)
|
||||
{
|
||||
return $q;
|
||||
}
|
||||
|
||||
$search = mb_strtolower(trim($search));
|
||||
$words = explode(' ', $search);
|
||||
preg_match_all('/(?:")((?:\\\\.|[^\\\\"])*)(?:")|(\S+)/', $search, $matches);
|
||||
$words = $matches[1];
|
||||
for ($i = 2; $i < count($matches); $i++) {
|
||||
$words = array_filter($words) + $matches[$i];
|
||||
}
|
||||
|
||||
$selects = [];
|
||||
$this->search_bindings = [];
|
||||
@@ -72,7 +75,9 @@ trait SearchableTrait
|
||||
|
||||
foreach ($queries as $select)
|
||||
{
|
||||
$selects[] = $select;
|
||||
if (!empty($select)) {
|
||||
$selects[] = $select;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,12 +88,12 @@ trait SearchableTrait
|
||||
$threshold = $relevance_count / 4;
|
||||
}
|
||||
|
||||
$this->filterQueryWithRelevance($query, $selects, $threshold);
|
||||
if (!empty($selects)) {
|
||||
$this->filterQueryWithRelevance($query, $selects, $threshold);
|
||||
}
|
||||
|
||||
$this->makeGroupBy($query);
|
||||
|
||||
$this->addBindingsToQuery($query, $this->search_bindings);
|
||||
|
||||
if(is_callable($restriction)) {
|
||||
$query = $restriction($query);
|
||||
}
|
||||
@@ -173,7 +178,7 @@ trait SearchableTrait
|
||||
$query->leftJoin($table, function ($join) use ($keys) {
|
||||
$join->on($keys[0], '=', $keys[1]);
|
||||
if (array_key_exists(2, $keys) && array_key_exists(3, $keys)) {
|
||||
$join->where($keys[2], '=', $keys[3]);
|
||||
$join->whereRaw($keys[2] . ' = "' . $keys[3] . '"');
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -219,8 +224,9 @@ trait SearchableTrait
|
||||
*/
|
||||
protected function addSelectsToQuery(Builder $query, array $selects)
|
||||
{
|
||||
$selects = new Expression('max(' . implode(' + ', $selects) . ') as relevance');
|
||||
$query->addSelect($selects);
|
||||
if (!empty($selects)) {
|
||||
$query->selectRaw('max(' . implode(' + ', $selects) . ') as relevance', $this->search_bindings);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -236,7 +242,7 @@ trait SearchableTrait
|
||||
|
||||
$relevance_count=number_format($relevance_count,2,'.','');
|
||||
|
||||
$query->havingRaw("$comparator > $relevance_count");
|
||||
$query->havingRaw("$comparator >= $relevance_count");
|
||||
$query->orderBy('relevance', 'desc');
|
||||
|
||||
// add bindings to postgres
|
||||
@@ -308,22 +314,6 @@ trait SearchableTrait
|
||||
return '(case when ' . $field . ' then ' . $relevance . ' else 0 end)';
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the bindings to the query.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param array $bindings
|
||||
*/
|
||||
protected function addBindingsToQuery(Builder $query, array $bindings) {
|
||||
$count = $this->getDatabaseDriver() != 'mysql' ? 2 : 1;
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
foreach($bindings as $binding) {
|
||||
$type = $i == 0 ? 'select' : 'having';
|
||||
$query->addBinding($binding, $type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge our cloned query builder with the original one.
|
||||
*
|
||||
@@ -337,6 +327,16 @@ trait SearchableTrait
|
||||
} else {
|
||||
$original->from(DB::connection($this->connection)->raw("({$clone->toSql()}) as `{$tableName}`"));
|
||||
}
|
||||
$original->mergeBindings($clone->getQuery());
|
||||
|
||||
// First create a new array merging bindings
|
||||
$mergedBindings = array_merge_recursive(
|
||||
$clone->getBindings(),
|
||||
$original->getBindings()
|
||||
);
|
||||
|
||||
// Then apply bindings WITHOUT global scopes which are already included. If not, there is a strange behaviour
|
||||
// with some scope's bindings remaning
|
||||
$original->withoutGlobalScopes()->setBindings($mergedBindings);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user