Home > Software design >  Laravel 9.x — Character encoding issue returning incorrect results for where() query
Laravel 9.x — Character encoding issue returning incorrect results for where() query

Time:10-05

BACKGROUND — I'm creating an Arabic-English dictionary that uses transliterations as the unique identifiers for terms (e.g. to distinguish between أكل /ʔakl/ & أكل /ʔakal/). Many Arabic letters don't have Latin-script equivalents, so I use certain special characters like āēīōū, ṣḍṭẓ & so on.

I have the following query:

$asInflection = Inflection::where('translit', $term->slug);

I've just noticed that my use of special characters returns incorrect query results.

In one case, the $term->slug is ʔamal & there is an Inflection::where('translit', ʔāmāl). Laravel returns a match, which it should not; it's absolutely imperative for the dictionary's proper functioning that these characters not be treated the same. I'm not sure if the issue is with PHP or with MySQL; I'm pretty sure the issue nothing to do with Laravel itself, but I imagine there is something I can do via Laravel to solve it.

Any advice is appreciated.

CodePudding user response:

I think there is a character recognition problem from the Database.

if you want to match some speacial charcters then you should use utf8_unicode_ci type of Collation for translit type column instead of utf8_general_ci.

So here you need to replace column collation type from utf8_general_ci to utf8_unicode_ci

CodePudding user response:

You can try this:

$asInflection = Inflection::where('translit', $term->slug)->get();
  • Related