spatie / laravel-translatable

Making Eloquent models translatable

Home Page:https://spatie.be/docs/laravel-translatable

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

json binary and collation

carlituxman opened this issue · comments

When use json field on mysql hasn't collation because is binary, but in mariadb json migrate create a longText field with collation.

We have a utf8mb4_unicode_ci (case insensitive)

Then doing a where like %$search% with $search = 'sesión', $search = 'sesion', $search = 'Sesión' should return same results, with mariadb works, but mysql not. We needed extends Illuminate\Database\Eloquent\Builder with this:

`public function whereCollateJsonLike($column, $value = null, $or = false)
{
$operator = 'like';
$searchOperator = $or ? 'orWhereRaw' : 'whereRaw';

    $collation = $this->getConnection()->getConfig()['collation'];
    $appLocale = app()->getLocale();

    $searchPattern = '%' . $value . '%';
    
    // return self::{$searchOperator}($column . '->"$.' . $appLocale . '" ' . $operator . '?', $searchPattern);
    return self::{$searchOperator}($column . '->"$.' . $appLocale . '" COLLATE ' . $collation . ' ' . $operator . '?', $searchPattern);
}`

To specify collation on search. too much tangled

I share with you because it's been a headache