diff --git a/.travis.yml b/.travis.yml index 8331ca2..ca5bcde 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,7 +17,6 @@ env: - setup=stable laravel=^5.8 - setup=stable laravel=^6.0 - setup=stable laravel=^7.0 - - setup=lowest laravel=^5.8 - setup=lowest laravel=^6.0 - setup=lowest laravel=^7.0 diff --git a/src/Searchable.php b/src/Searchable.php index 433a8fc..809bd23 100644 --- a/src/Searchable.php +++ b/src/Searchable.php @@ -100,6 +100,10 @@ protected function queryOnColumnUsing( $keywords = $this->keyword->all(); + if ($query instanceof EloquentBuilder) { + $column = $query->qualifyColumn((string) $column); + } + return $query->{$whereOperator}(static function ($query) use ($column, $keywords, $likeOperator) { foreach ($keywords as $keyword) { $query->orWhere((string) $column, $likeOperator, $keyword); diff --git a/tests/Feature/EloquentSearchableTest.php b/tests/Feature/EloquentSearchableTest.php index f1d751a..8da3170 100644 --- a/tests/Feature/EloquentSearchableTest.php +++ b/tests/Feature/EloquentSearchableTest.php @@ -21,7 +21,7 @@ public function it_can_build_search_query() $stub->apply($query); $this->assertSame( - 'select * from "users" where (("name" like ? or "name" like ? or "name" like ? or "name" like ?))', + 'select * from "users" where (("users"."name" like ? or "users"."name" like ? or "users"."name" like ? or "users"."name" like ?))', $query->toSql() ); @@ -161,19 +161,19 @@ public function it_cant_build_search_query_with_invalid_column_name() public function it_can_build_search_query_with_relation_field() { $stub = new Searchable( - 'hello', ['posts.title'] + 'hello', ['name', 'posts.title'] ); $query = User::query(); $stub->apply($query); $this->assertSame( - 'select * from "users" where (exists (select * from "posts" where "users"."id" = "posts"."user_id" and ("title" like ? or "title" like ? or "title" like ? or "title" like ?)))', + 'select * from "users" where (("users"."name" like ? or "users"."name" like ? or "users"."name" like ? or "users"."name" like ?) or exists (select * from "posts" where "users"."id" = "posts"."user_id" and ("posts"."title" like ? or "posts"."title" like ? or "posts"."title" like ? or "posts"."title" like ?)))', $query->toSql() ); $this->assertSame( - ['hello', 'hello%', '%hello', '%hello%'], + ['hello', 'hello%', '%hello', '%hello%', 'hello', 'hello%', '%hello', '%hello%'], $query->getBindings() ); } diff --git a/tests/Feature/EloquentTaxanomyTest.php b/tests/Feature/EloquentTaxanomyTest.php index 81645a2..b965698 100644 --- a/tests/Feature/EloquentTaxanomyTest.php +++ b/tests/Feature/EloquentTaxanomyTest.php @@ -82,7 +82,7 @@ public function it_can_build_match_query_with_basic_search() $stub->apply($query); $this->assertSame( - 'select * from "users" where (("name" like ? or "name" like ? or "name" like ? or "name" like ?))', + 'select * from "users" where (("users"."name" like ? or "users"."name" like ? or "users"."name" like ? or "users"."name" like ?))', $query->toSql() ); @@ -91,4 +91,25 @@ public function it_can_build_match_query_with_basic_search() $query->getBindings() ); } + + /** @test */ + public function it_can_build_match_query_with_basic_search_with_related_field() + { + $stub = new Taxonomy( + 'hello', [], ['name', 'posts.title'] + ); + + $query = User::query(); + $stub->apply($query); + + $this->assertSame( + 'select * from "users" where (("users"."name" like ? or "users"."name" like ? or "users"."name" like ? or "users"."name" like ?) or exists (select * from "posts" where "users"."id" = "posts"."user_id" and ("posts"."title" like ? or "posts"."title" like ? or "posts"."title" like ? or "posts"."title" like ?)))', + $query->toSql() + ); + + $this->assertSame( + ['hello', 'hello%', '%hello', '%hello%', 'hello', 'hello%', '%hello', '%hello%'], + $query->getBindings() + ); + } }