From 0f3972da6aa2034037aaa060883a7d5d4e796d00 Mon Sep 17 00:00:00 2001 From: Davide Bellini Date: Wed, 22 Oct 2014 16:51:18 +0200 Subject: [PATCH] Small improvements --- .../Searchable/SearchableTrait.php | 21 ++++++------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/src/Nicolaslopezj/Searchable/SearchableTrait.php b/src/Nicolaslopezj/Searchable/SearchableTrait.php index de4dce2..60aee0f 100755 --- a/src/Nicolaslopezj/Searchable/SearchableTrait.php +++ b/src/Nicolaslopezj/Searchable/SearchableTrait.php @@ -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); @@ -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', []); } /** @@ -93,8 +85,7 @@ protected function makeJoins(&$query) */ protected function makeGroupBy(&$query) { - $primary_key = $this->primaryKey; - $query->groupBy($primary_key); + $query->groupBy($this->primaryKey); } /** @@ -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); } @@ -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)'; }