Skip to content

Commit

Permalink
skip adding search constraints with empty search (#582)
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigopedra authored Feb 8, 2022
1 parent d331da3 commit 3ec268e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Engines/DatabaseEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ protected function buildSearchQuery(Builder $builder)
*/
protected function initializeSearchQuery(Builder $builder, array $columns, array $prefixColumns = [], array $fullTextColumns = [])
{
if (blank($builder->query)) {
return $builder->model->query();
}

return $builder->model->query()->where(function ($query) use ($builder, $columns, $prefixColumns, $fullTextColumns) {
$connectionType = $builder->model->getConnection()->getDriverName();

Expand Down
14 changes: 14 additions & 0 deletions tests/Feature/DatabaseEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ public function test_it_can_retrieve_results_with_empty_search()
$this->assertCount(2, $models);
}

public function test_it_does_not_add_search_where_clauses_with_empty_search()
{
SearchableUserDatabaseModel::search('')->query(function ($builder) {
$this->assertSame('select * from "users"', $builder->toSql());
})->get();
}

public function test_it_adds_search_where_clauses_with_non_empty_search()
{
SearchableUserDatabaseModel::search('Taylor')->query(function ($builder) {
$this->assertSame('select * from "users" where ("users"."id" like ? or "users"."name" like ? or "users"."email" like ?)', $builder->toSql());
})->get();
}

public function test_it_can_retrieve_results()
{
$models = SearchableUserDatabaseModel::search('Taylor')->where('email', 'taylor@laravel.com')->get();
Expand Down

0 comments on commit 3ec268e

Please sign in to comment.