Skip to content

Commit

Permalink
Small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Davide Bellini committed Oct 22, 2014
1 parent 187867e commit 0f3972d
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions src/Nicolaslopezj/Searchable/SearchableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,9 @@ public function scopeSearch($query, $search)
foreach ($this->getColumns() as $column => $relevance)
{
$relevance_count += $relevance;
$queries = $this->getSearchQueriesForColumn($column, $relevance, $words);

foreach ($queries as $select)
{
$selects[] = $select;
}
$queries = $this->getSearchQueriesForColumn($column, $relevance, $words);
$selects = array_merge($queries);
}

$this->addSelectsToQuery($query, $selects);
Expand Down Expand Up @@ -65,12 +62,7 @@ protected function getColumns()
*/
protected function getJoins()
{
if ( ! array_key_exists('joins', $this->searchable))
{
return [];
}

return $this->searchable['joins'];
return array_get($this->searchable, 'joins', []);
}

/**
Expand All @@ -93,8 +85,7 @@ protected function makeJoins(&$query)
*/
protected function makeGroupBy(&$query)
{
$primary_key = $this->primaryKey;
$query->groupBy($primary_key);
$query->groupBy($this->primaryKey);
}

/**
Expand All @@ -105,7 +96,7 @@ protected function makeGroupBy(&$query)
*/
protected function addSelectsToQuery(&$query, $selects)
{
$selects = new Expression(join(' + ', $selects) . ' as relevance');
$selects = new Expression(implode(' + ', $selects) . ' as relevance');
$query->addSelect($selects);
}

Expand Down Expand Up @@ -161,7 +152,7 @@ protected function getSearchQuery($column, $relevance, $words, $compare, $releva
$fields[] = $column . " " . $compare . " '" . $pre_word . $word . $post_word . "'";
}

$fields = join(' || ', $fields);
$fields = implode(' || ', $fields);

return 'if(' . $fields . ', ' . $relevance * $relevance_multiplier . ', 0)';
}
Expand Down

0 comments on commit 0f3972d

Please sign in to comment.