update v1.0.6

This commit is contained in:
sujitprasad
2016-02-16 22:42:08 +05:30
parent e6b579d67b
commit 073a49a8af
587 changed files with 21487 additions and 22766 deletions

View File

@@ -33,10 +33,10 @@ class User extends \Eloquent
*/
protected $searchable = [
'columns' => [
'first_name' => 10,
'last_name' => 10,
'bio' => 2,
'email' => 5,
'users.first_name' => 10,
'users.last_name' => 10,
'users.bio' => 2,
'users.email' => 5,
'posts.title' => 2,
'posts.body' => 1,
],

View File

@@ -9,6 +9,10 @@ use Illuminate\Support\Str;
/**
* Trait SearchableTrait
* @package Nicolaslopezj\Searchable
* @property array $searchable
* @property string $table
* @property string $primaryKey
* @method string getTable()
*/
trait SearchableTrait
{
@@ -202,7 +206,7 @@ trait SearchableTrait
*/
protected function addSelectsToQuery(Builder $query, array $selects)
{
$selects = new Expression('avg(' . implode(' + ', $selects) . ') as relevance');
$selects = new Expression('max(' . implode(' + ', $selects) . ') as relevance');
$query->addSelect($selects);
}
@@ -281,6 +285,11 @@ trait SearchableTrait
* @return string
*/
protected function getCaseCompare($column, $compare, $relevance) {
if($this->getDatabaseDriver() == 'pgsql') {
$field = "LOWER(" . $column . ") " . $compare . " ?";
return '(case when ' . $field . ' then ' . $relevance . ' else 0 end)';
}
$column = str_replace('.', '`.`', $column);
$field = "LOWER(`" . $column . "`) " . $compare . " ?";
return '(case when ' . $field . ' then ' . $relevance . ' else 0 end)';
@@ -309,7 +318,12 @@ trait SearchableTrait
* @param \Illuminate\Database\Eloquent\Builder $original
*/
protected function mergeQueries(Builder $clone, Builder $original) {
$original->from(DB::connection($this->connection)->raw("({$clone->toSql()}) as `{$this->getTable()}`"));
$tableName = DB::connection($this->connection)->getTablePrefix() . $this->getTable();
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}`"));
}
$original->mergeBindings($clone->getQuery());
}
}