Skip to content

Commit 3fc6180

Browse files
authored
Merge pull request #292 from RonasIT/289-fix-relation-query-search
[289]: add table prefix to SearchTrait filterByQuery
2 parents 44b397b + f5f49c6 commit 3fc6180

2 files changed

Lines changed: 14 additions & 12 deletions

File tree

src/Traits/SearchTrait.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public function withCount(array|string $relations): self
274274

275275
protected function getQuerySearchCallback(string $field, string $mask): Closure
276276
{
277-
return function ($query) use ($field, $mask) {
277+
return function (Query $query) use ($field, $mask) {
278278
$databaseDriver = config('database.default');
279279
$value = ($databaseDriver === 'pgsql')
280280
? pg_escape_string($this->filter['query'])
@@ -284,6 +284,8 @@ protected function getQuerySearchCallback(string $field, string $mask): Closure
284284
? 'ilike'
285285
: 'like';
286286

287+
$field = $query->qualifyColumn($field);
288+
287289
$query->orWhere($field, $operator, DB::raw($value));
288290
};
289291
}

tests/support/Traits/SqlMockTrait.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -500,13 +500,13 @@ protected function mockGetSearchResultWithQuery(array $selectResult): void
500500
{
501501
$this->mockSelectWithAggregate(
502502
'select count(*) as aggregate from "test_models" '
503-
. "where ((\"query_field\" like '%search_\'string%') or (\"another_query_field\" like '%search_\'string%')) "
503+
. "where ((\"test_models\".\"query_field\" like '%search_\'string%') or (\"test_models\".\"another_query_field\" like '%search_\'string%')) "
504504
. 'and "test_models"."deleted_at" is null',
505505
);
506506

507507
$this->mockSelect(
508-
"select * from \"test_models\" where ((\"query_field\" like '%search_\'string%') "
509-
. "or (\"another_query_field\" like '%search_\'string%')) and \"test_models\".\"deleted_at\" is null "
508+
"select * from \"test_models\" where ((\"test_models\".\"query_field\" like '%search_\'string%') "
509+
. "or (\"test_models\".\"another_query_field\" like '%search_\'string%')) and \"test_models\".\"deleted_at\" is null "
510510
. 'order by "id" asc limit 15 offset 0',
511511
$selectResult,
512512
);
@@ -516,15 +516,15 @@ protected function mockGetSearchResultWithCustomQuery(array $selectResult): void
516516
{
517517
$this->mockSelectWithAggregate(
518518
'select count(*) as aggregate from "test_models" '
519-
. 'where (("query_field"::text ilike \'%\' || unaccent(\'search_\'\'string\') || \'%\') '
520-
. 'or ("another_query_field"::text ilike \'%\' || unaccent(\'search_\'\'string\') || \'%\')) '
519+
. 'where (("test_models"."query_field"::text ilike \'%\' || unaccent(\'search_\'\'string\') || \'%\') '
520+
. 'or ("test_models"."another_query_field"::text ilike \'%\' || unaccent(\'search_\'\'string\') || \'%\')) '
521521
. 'and "test_models"."deleted_at" is null',
522522
);
523523

524524
$this->mockSelect(
525525
'select * from "test_models" '
526-
. 'where (("query_field"::text ilike \'%\' || unaccent(\'search_\'\'string\') || \'%\') '
527-
. 'or ("another_query_field"::text ilike \'%\' || unaccent(\'search_\'\'string\') || \'%\')) '
526+
. 'where (("test_models"."query_field"::text ilike \'%\' || unaccent(\'search_\'\'string\') || \'%\') '
527+
. 'or ("test_models"."another_query_field"::text ilike \'%\' || unaccent(\'search_\'\'string\') || \'%\')) '
528528
. 'and "test_models"."deleted_at" is null order by "id" asc limit 15 offset 0',
529529
$selectResult,
530530
);
@@ -534,9 +534,9 @@ protected function mockGetSearchResultWithRelations(array $selectResult): void
534534
{
535535
$this->mockSelectWithAggregate(
536536
'select count(*) as aggregate from "test_models" '
537-
. 'where (("query_field" like \'%search_string%\') or exists (select * from "relation_models" '
537+
. 'where (("test_models"."query_field" like \'%search_string%\') or exists (select * from "relation_models" '
538538
. 'where "test_models"."id" = "relation_models"."test_model_id" '
539-
. 'and ("another_query_field" like \'%search_string%\'))) and exists (select * from "relation_models" '
539+
. 'and ("relation_models"."another_query_field" like \'%search_string%\'))) and exists (select * from "relation_models" '
540540
. 'where "test_models"."id" = "relation_models"."test_model_id" and "name" = ?) '
541541
. 'and "test_models"."deleted_at" is null',
542542
['some_value'],
@@ -545,10 +545,10 @@ protected function mockGetSearchResultWithRelations(array $selectResult): void
545545
$this->mockSelect(
546546
'select "test_models".*, (select "id" from "relation_models" '
547547
. 'where "test_models"."id" = "relation_models"."test_model_id" order by "id" asc limit 1) '
548-
. 'as "relation_id" from "test_models" where (("query_field" like \'%search_string%\') '
548+
. 'as "relation_id" from "test_models" where (("test_models"."query_field" like \'%search_string%\') '
549549
. 'or exists (select * from "relation_models" '
550550
. 'where "test_models"."id" = "relation_models"."test_model_id" '
551-
. 'and ("another_query_field" like \'%search_string%\'))) and '
551+
. 'and ("relation_models"."another_query_field" like \'%search_string%\'))) and '
552552
. 'exists (select * from "relation_models" where "test_models"."id" = "relation_models"."test_model_id" '
553553
. 'and "name" = ?) and "test_models"."deleted_at" is null '
554554
. 'order by "relation_id" asc, "id" asc limit 15 offset 0',

0 commit comments

Comments
 (0)