diff --git a/src/Database/Builder.php b/src/Database/Builder.php index 3c4af0d1..9b64c0b2 100644 --- a/src/Database/Builder.php +++ b/src/Database/Builder.php @@ -88,6 +88,9 @@ protected function searchWhereInternal($term, $columns, $mode, $boolean) if (!strlen($term)) { continue; } + if ($field instanceof Expression) { + $field = $field->getValue($query->getQuery()->getGrammar()); + } $fieldSql = $this->query->raw(sprintf("lower(%s)", DbDongle::cast($field, 'text'))); $termSql = '%' . trim(mb_strtolower($term)) . '%'; $query->orWhere($fieldSql, 'LIKE', $termSql); diff --git a/tests/Database/ModelTest.php b/tests/Database/ModelTest.php index 94ed4064..625c5a68 100644 --- a/tests/Database/ModelTest.php +++ b/tests/Database/ModelTest.php @@ -133,6 +133,33 @@ protected function seedTables() $this->seeded['websites'][1]->users()->add($this->seeded['users'][1]); } + public function testSearchWhereExactHandlesExpressionColumns() + { + $grammar = Post::query()->getQuery()->getGrammar(); + $relationColumn = DB::raw(sprintf( + '(select %s from %s where %s = %s)', + $grammar->wrap('users.name'), + $grammar->wrapTable('users'), + $grammar->wrap('users.id'), + $grammar->wrap('posts.user_id') + )); + + $resultIds = Post::query() + ->searchWhere('User1', [$relationColumn], 'exact') + ->pluck('id') + ->all(); + + sort($resultIds); + + $expectedIds = [ + $this->seeded['posts'][0]->id, + $this->seeded['posts'][1]->id, + ]; + sort($expectedIds); + + $this->assertEquals($expectedIds, $resultIds); + } + // tests hasOneThrough & hasManyThrough public function testDeleteWithThroughRelations() {