filamentphp / filament

A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS.
https://filamentphp.com
MIT License
18.1k stars 2.83k forks source link

Spatie Translatable Listing column searchable exception in sql query when searching #9649

Closed dev3k closed 10 months ago

dev3k commented 10 months ago

Package

filament/filament

Package Version

v3.0.95

Laravel Version

v10.31.0

Livewire Version

v3.1.0

PHP Version

PHP 8.2.9

Problem description

When you search a translatable column I get the following error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'json_extract(title, "$.en")' in 'where clause'

SELECT count(*) AS aggregate FROM `posts` WHERE (`json_extract(title, "$`.`en")` LIKE %AA%)

Expected behavior

it should be like

SELECT count(*) AS aggregate FROM `posts` WHERE json_extract(`title`, "$.en") LIKE '%AA%'

Steps to reproduce

just visit http://json-column.test/admin/posts?tableSearch=AA

Reproduction repository

https://github.com/dev3k/json-column

Relevant log output

No response

dev3k commented 10 months ago

similar to #7537

dev3k commented 10 months ago

I did some debugging and found this https://github.com/filamentphp/filament/commit/c89f86bad00dc42dca8fa467b21d48e2c66687ea PR https://github.com/filamentphp/filament/pull/8784

search_collation always return null on mysql

$databaseConnection->getConfig('search_collation')

to make it work it should've been 'collation' instead of 'search_collation'

$databaseConnection->getConfig('collation')

I dont know why it's 'search_collation' maybe postgres ? I never used it before 😄

Screenshot 2023-11-16 at 8 42 31 PM

Ahmant commented 10 months ago

Hello, Same issue here.

But to address the issue (until it is resolved by the package), I performed the search query independently, as follows:

public static function table(Table $table): Table
{
    $activeLocale = $table->getLivewire()->activeLocale ?? 'en'; // 👈 Here

    return $table
        ->columns([
            TextColumn::make('title')
                // 👇 Here
                ->searchable(query: fn (Builder $query, string $search) => $query->whereRaw("JSON_EXTRACT(LOWER(title), '$.$activeLocale') LIKE LOWER('%$search%')")) // 👈 Here
        ])
}
zumbidoweb commented 10 months ago

+1

Screenshot 2023-11-17 at 12 19 51 PM

inalto commented 10 months ago

please look @ my pull request https://github.com/filamentphp/filament/pull/9732

this fixed the issue for me