From 073799dc4e9d4a93f9a922d11ea51bf0c8f1a036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Pardellas=20Blunier?= Date: Mon, 19 Sep 2016 12:07:38 +0200 Subject: [PATCH] Fixed search criteria. Its conditions must be within parentheses --- src/Repository/Criteria/SearchCriteria.php | 26 ++++++++++++---------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/Repository/Criteria/SearchCriteria.php b/src/Repository/Criteria/SearchCriteria.php index acf8c02..edbc836 100644 --- a/src/Repository/Criteria/SearchCriteria.php +++ b/src/Repository/Criteria/SearchCriteria.php @@ -17,21 +17,23 @@ public function __construct(array $columns, $queryString) public function apply($model, Repository $repository) { - $firstColumn = array_shift($this->columns); + $query = $model->where(function ($query) use ($repository) { + $firstColumn = array_shift($this->columns); - if (strpos($firstColumn, '.')) { - $query = $this->setRelationFieldCondition($model, $firstColumn, false); - } else { - $query = $model->where($firstColumn, 'LIKE', '%'.$this->queryString.'%'); - } - - foreach ($this->columns as $column) { - if (strpos($column, '.')) { - $query = $this->setRelationFieldCondition($query, $column); + if (strpos($firstColumn, '.')) { + $query = $this->setRelationFieldCondition($query, $firstColumn, false); } else { - $query->orWhere($column, 'LIKE', '%'.$this->queryString.'%'); + $query = $query->where($firstColumn, 'LIKE', '%'.$this->queryString.'%'); } - } + + foreach ($this->columns as $column) { + if (strpos($column, '.')) { + $query = $this->setRelationFieldCondition($query, $column); + } else { + $query->orWhere($column, 'LIKE', '%'.$this->queryString.'%'); + } + } + }); return $query; }