tailflow / laravel-orion

The simplest way to create REST API with Laravel
https://orion.tailflow.org
MIT License
1.5k stars 89 forks source link

Case-insensitive does not work with JSON field type #228

Open pakomp opened 1 year ago

pakomp commented 1 year ago

Hi,

An Exception will be thrown if setting search.case_sensitive = false, and having a JSON field as searchable. The field is JSON type due to it being translatable (using the package spatie/laravel-translatable).

Exception: SQLSTATE[42883]: Undefined function: 7 ERROR: function lower(json) does not exist

I'm using Postgresql

EDIT: The problem exists on other field types as well, Orion tries to do lower() on integer columns as well

OzanKurt commented 10 months ago

I made it work somehow:

vendor/tailflow/laravel-orion/src/Drivers/Standard/QueryBuilder.php@applySearchingToQueryL429

                    if (!$caseSensitive) {
                        if (str_contains($qualifiedFieldName, '->')) {
                            $grammar = $whereQuery->getGrammar();
                            $qualifiedFieldName = $grammar->wrap($qualifiedFieldName);

                        $whereQuery->orWhereRaw(
                            "lower({$qualifiedFieldName}) like lower(?)",
                            ['%'.$requestedSearchString.'%']
                        );
                        } else {
                        $whereQuery->orWhereRaw(
                            "lower({$qualifiedFieldName}) like lower(?)",
                            ['%'.$requestedSearchString.'%']
                        );
                        }
                    } else {
                        $whereQuery->orWhere(
                            $qualifiedFieldName,
                            'like',
                            '%'.$requestedSearchString.'%'
                        );
                    }