Skip to content

Commit

Permalink
fix broken bindings after searchable trait is applied
Browse files Browse the repository at this point in the history
  • Loading branch information
Tamrael committed Oct 2, 2017
1 parent c067f11 commit 98cbc59
Showing 1 changed file with 2 additions and 26 deletions.
28 changes: 2 additions & 26 deletions src/SearchableTrait.php
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -51,7 +50,7 @@ public function scopeSearchRestricted(Builder $q, $search, $restriction, $thresh
preg_match_all('/(?:")((?:\\\\.|[^\\\\"])*)(?:")|(\S+)/', $search, $matches);
$words = $matches[1];
for ($i = 2; $i < count($matches); $i++) {
$words = array_filter($words) + $matches[$i];
$words = array_filter($words) + $matches[$i];
}

$selects = [];
Expand Down Expand Up @@ -91,12 +90,6 @@ public function scopeSearchRestricted(Builder $q, $search, $restriction, $thresh

$this->makeGroupBy($query);

$clone_bindings = $query->getBindings();
$query->setBindings([]);

$this->addBindingsToQuery($query, $this->search_bindings);
$this->addBindingsToQuery($query, $clone_bindings);

if(is_callable($restriction)) {
$query = $restriction($query);
}
Expand Down Expand Up @@ -227,8 +220,7 @@ protected function makeGroupBy(Builder $query)
*/
protected function addSelectsToQuery(Builder $query, array $selects)
{
$selects = new Expression('max(' . implode(' + ', $selects) . ') as relevance');
$query->addSelect($selects);
$query->selectRaw('max(' . implode(' + ', $selects) . ') as relevance', $this->search_bindings);
}

/**
Expand Down Expand Up @@ -316,22 +308,6 @@ protected function getCaseCompare($column, $compare, $relevance) {
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 == 1 ? 'select' : 'having';
$query->addBinding($binding, $type);
}
}
}

/**
* Merge our cloned query builder with the original one.
*
Expand Down

0 comments on commit 98cbc59

Please sign in to comment.