Skip to content

Commit

Permalink
Merge pull request #5 from ajcastro/1.3
Browse files Browse the repository at this point in the history
Add enableSearchable(), disableSearchable() methods, Fix setSearchabl…
  • Loading branch information
ajcastro authored Aug 2, 2019
2 parents 0e2b55d + a46417a commit c275f31
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
12 changes: 9 additions & 3 deletions src/Search/SublimeSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,22 @@ public function columnKeys()
*/
public function search($searchStr)
{
$conditions = [];
$columnsToCompare = $this->columnsToCompare();
$conditions = [];
$query = $this->query();

if (count($columnsToCompare) === 0) {
return $query;
}

$parsedStr = $this->parseSearchStr($this->searchStr = $searchStr);

foreach ($this->columnsToCompare() as $column) {
foreach ($columnsToCompare as $column) {
$conditions[] = $column.' like "'.$parsedStr.'"';
}

$method = $this->searchOperator.'Raw';
$query = $this->query()->{$method}('('.join(' OR ', $conditions).')');
$query->{$method}('('.join(' OR ', $conditions).')');

if ($this->shouldSortByRelevance()) {
$this->applySortByRelevance();
Expand Down
45 changes: 34 additions & 11 deletions src/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ trait Searchable
{
protected static $allSearchableColumns = [];

protected $searchableEnabled = true;

protected $sortByRelevance = true;

protected $searchQuery;
Expand Down Expand Up @@ -136,6 +138,28 @@ public function setSearchQuery($searchQuery)
return $this;
}

/**
* Disable searchable in the model.
*
* @return $this
*/
public function disableSearchable()
{
$this->searchableEnabled = false;
return $this;
}

/**
* Enable searchable in the model.
*
* @return $this
*/
public function enableSearchable()
{
$this->searchableEnabled = true;
return $this;
}

/**
* Apply search in the query.
*
Expand All @@ -146,6 +170,10 @@ public function setSearchQuery($searchQuery)
*/
public function scopeSearch($query, $search)
{
if (!$this->searchableEnabled) {
return;
}

$this->applySearchableJoins($query);

if (empty($query->getQuery()->columns)) {
Expand Down Expand Up @@ -199,13 +227,8 @@ public function shouldSortByRelevance()
*/
public function setSearchable($config)
{
if ($columns = array_get($config, 'columns')) {
$this->setSearchableColumns($columns);
}

if ($joins = array_get($config, 'joins')) {
$this->setSearchableJoins($joins);
}
$this->setSearchableColumns(array_get($config, 'columns'));
$this->setSearchableJoins(array_get($config, 'joins'));

return $this;
}
Expand All @@ -219,11 +242,11 @@ public function setSearchable($config)
public function setSearchableColumns($columns)
{
if (property_exists($this, 'searchableColumns')) {
$this->searchableColumns = $columns;
$this->searchableColumns = $columns ?? [];
}

if (property_exists($this, 'searchable')) {
$this->searchable['columns'] = $columns;
$this->searchable['columns'] = $columns ?? [];
}

return $this;
Expand All @@ -238,11 +261,11 @@ public function setSearchableColumns($columns)
public function setSearchableJoins($joins)
{
if (property_exists($this, 'searchableJoins')) {
$this->searchableJoins = $joins;
$this->searchableJoins = $joins ?? [];
}

if (property_exists($this, 'searchable')) {
$this->searchable['joins'] = $joins;
$this->searchable['joins'] = $joins ?? [];
}

return $this;
Expand Down

0 comments on commit c275f31

Please sign in to comment.