Jeremy
Jeremy

Reputation: 1952

Laravel and package spatie/laravel-translatable -> Make search query for special character

I use the package Spatie/laravel-translatable to store my translated data in json in the database.

I have a column title :

{
  "fr":"L\u2019\u00e9quipe",
  "en":"Team",
  "de":"Team"
}

As you can see, with the package, special characters are transformed into unicode.

When I want to search, it does not work (it's logical, because I'm looking for the word équipe but in the database it says \u00e9quipe)

$search = $request->get('search')

Events::where('title', 'LIKE', '%' . $search . '%')->published()->get();

How to search through Unicode for it to work?

Thank you

Upvotes: 5

Views: 2501

Answers (2)

bhdrnzl
bhdrnzl

Reputation: 61

$search = $request->get('search');

Events::whereRaw('title COLLATE utf8mb4_unicode_ci LIKE ?', ['%' . $search . '%'])
    ->published()
    ->get();

Upvotes: -2

Taha Elkholy
Taha Elkholy

Reputation: 11

in the Spatie\Translatable\HasTranslations trait use $this->attributes[$key] = json_encode($translations, JSON_UNESCAPED_UNICODE);

instead of $this->attributes[$key] = $this->asJson($translations);

Upvotes: 1

Related Questions