Created
August 17, 2019 09:07
-
-
Save rolldone/a24f724024e77bd1f8a3bf019189199e to your computer and use it in GitHub Desktop.
Improve bug Searchable, extend on it on your new laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Traits; | |
use Illuminate\Database\Eloquent\Builder; | |
use Illuminate\Support\Facades\Config; | |
use Illuminate\Support\Facades\DB; | |
use Illuminate\Support\Str; | |
use Nicolaslopezj\Searchable\SearchableTrait as ParentSearchableTraits; | |
/** | |
* Trait SearchableTrait | |
* @package Nicolaslopezj\Searchable | |
* @property array $searchable | |
* @property string $table | |
* @property string $primaryKey | |
* @method string getTable() | |
*/ | |
trait SearchableTrait | |
{ | |
use ParentSearchableTraits; | |
/** | |
* Makes the query not repeat the results. | |
* | |
* @param \Illuminate\Database\Eloquent\Builder $query | |
*/ | |
protected function makeGroupBy(Builder $query) | |
{ | |
if ($groupBy = $this->getGroupBy()) { | |
$query->groupBy($groupBy); | |
} else { | |
$driver = $this->getDatabaseDriver(); | |
if ($driver == 'sqlsrv') { | |
$columns = $this->getTableColumns(); | |
} else { | |
$columns = $this->getTable() . '.' .$this->primaryKey; | |
} | |
$query->groupBy($columns); | |
$joins = array_keys(($this->getJoins())); | |
foreach ($this->getColumns() as $column => $relevance) { | |
array_map(function ($join) use ($column, $query) { | |
if (Str::contains($column, $join)) { | |
$whatIWant = ''; | |
if (($pos = strpos($column, ".")) !== FALSE) { | |
$whatIWant = substr($column, $pos+1); | |
} | |
$query->groupBy($join.'.'.$whatIWant); | |
} | |
}, $joins); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment