Skip to content

Commit

Permalink
Merge pull request #7 from laravie/related-ambiguous
Browse files Browse the repository at this point in the history
Fixes column related clause is ambiguous. Fixes #6
  • Loading branch information
crynobone committed Jul 25, 2020
2 parents 32115b3 + cab3557 commit da29d8f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions src/Searchable.php
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions tests/Feature/EloquentSearchableTest.php
Expand Up @@ -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()
);

Expand Down Expand Up @@ -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()
);
}
Expand Down
23 changes: 22 additions & 1 deletion tests/Feature/EloquentTaxanomyTest.php
Expand Up @@ -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()
);

Expand All @@ -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()
);
}
}

0 comments on commit da29d8f

Please sign in to comment.