3

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

Jeremy
  • 1,756
  • 3
  • 21
  • 45
  • 1
    If the column title field contains the raw value `L\u2019\u00e9quipe`, that is a string containing escaped characters. Why don't you use UTF8 all the way through? – Nico Haase Feb 01 '19 at 13:18

2 Answers2

0

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

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

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 16 '22 at 02:19
-2
$search = $request->get('search');

Events::whereRaw('title COLLATE utf8mb4_unicode_ci LIKE ?', ['%' . $search . '%'])
    ->published()
    ->get();
OMi Shah
  • 5,768
  • 3
  • 25
  • 34
bhdrnzl
  • 61
  • 1
  • 3
  • 7
  • 1
    Probably you should add a little more info to add some value to your answer and make it more useful for future readers ! – OMi Shah Feb 28 '23 at 17:20