diff --git a/packages/support/src/helpers.php b/packages/support/src/helpers.php index a8889221e4..7da3a2782b 100644 --- a/packages/support/src/helpers.php +++ b/packages/support/src/helpers.php @@ -245,6 +245,10 @@ if (! function_exists('Filament\Support\generate_search_column_expression')) { { $driverName = $databaseConnection->getDriverName(); + if ($driverName === 'pgsql' && str_contains($column, '.')) { + $column = $databaseConnection->getTablePrefix() . $column; + } + $column = match ($driverName) { 'pgsql' => ( str($column)->contains('->') diff --git a/tests/src/Support/helpersTest.php b/tests/src/Support/helpersTest.php index cae67fc252..d8d086e081 100644 --- a/tests/src/Support/helpersTest.php +++ b/tests/src/Support/helpersTest.php @@ -111,6 +111,7 @@ it('will generate a column expression for Postgres with colons in the table name $databaseConnection = Mockery::mock(Connection::class); $databaseConnection->shouldReceive('getDriverName')->andReturn('pgsql'); + $databaseConnection->shouldReceive('getTablePrefix')->andReturn(''); $databaseConnection->shouldReceive('getConfig')->with('search_collation')->andReturn(null); $grammar = new PostgresGrammar($databaseConnection); @@ -125,6 +126,26 @@ it('will generate a column expression for Postgres with colons in the table name ['blog:posts:comments.author.name', 'lower("blog:posts:comments"."author"."name"::text)'], ]); +it('will generate a search column expression for Postgres with a table prefix', function (string $column, string $expectedExpression): void { + $isSearchForcedCaseInsensitive = true; + + $databaseConnection = Mockery::mock(Connection::class); + $databaseConnection->shouldReceive('getDriverName')->andReturn('pgsql'); + $databaseConnection->shouldReceive('getTablePrefix')->andReturn('app_'); + $databaseConnection->shouldReceive('getConfig')->with('search_collation')->andReturn(null); + + $grammar = new PostgresGrammar($databaseConnection); + + $actualExpression = generate_search_column_expression($column, $isSearchForcedCaseInsensitive, $databaseConnection); + + expect($actualExpression->getValue($grammar)) + ->toBe($expectedExpression); +}) + ->with([ + ['posts.title', 'lower("app_posts"."title"::text)'], + ['posts.data->name', "lower(\"app_posts\".\"data\"->>'name'::text)"], + ]); + it('will generate a JSON search column expression for Postgres with explicit ->> operator', function (): void { $column = 'data->>name'; $isSearchForcedCaseInsensitive = true;