From 45a2932f4bc87647ff3dd56a79685e1400ff673a Mon Sep 17 00:00:00 2001 From: Till Deeke Date: Fri, 20 Jul 2018 22:23:29 +0200 Subject: [PATCH] Fixes the generation of where conditions (#5902) --- app/Models/Traits/Searchable.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/app/Models/Traits/Searchable.php b/app/Models/Traits/Searchable.php index b6438b824d..01c20febda 100644 --- a/app/Models/Traits/Searchable.php +++ b/app/Models/Traits/Searchable.php @@ -68,6 +68,8 @@ trait Searchable { $table = $this->getTable(); + $firstConditionAdded = false; + foreach($this->getSearchableAttributes() as $column) { foreach($terms as $term) { @@ -80,6 +82,19 @@ trait Searchable { continue; } + /** + * We need to form the query properly, starting with a "where", + * otherwise the generated select is wrong. + * + * @todo This does the job, but is inelegant and fragile + */ + if (!$firstConditionAdded) { + $query = $query->where($table . '.' . $column, 'LIKE', '%'.$term.'%'); + + $firstConditionAdded = true; + continue; + } + $query = $query->orWhere($table . '.' . $column, 'LIKE', '%'.$term.'%'); } }