0

I have a laravel 5.5 setup with mariadb 10.0.32. Database encoding utf8mb4 (utf8mb4_unicode_ci).

and this snippet of code:

$letter  = "К";
$countries = DB::table('country_translations')->where([
    ['name', 'like', $letter . "%"],
    ['locale', '=', "ru"]
])->get();

where $letter is cyrillic letter К.

If I output DB log it shows this:

Array
(
    [0] => Array
        (
            [query] => select * from `country_translations` where (`name` like ? and `locale` = ?)
            [bindings] => Array
                (
                    [0] => �%
                    [1] => ru
                )

            [time] => 2.07
        )
)

As you can see, Laravel somehow messes up encoding. And because of this query returns zero entries.

Can anybody suggest a solution for this problem?

Update config/databese.php:

'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],
kumold
  • 25
  • 7
  • check this ones https://stackoverflow.com/questions/30442687/how-to-override-default-escape-function-of-blade-in-laravel-5/30443736#30443736 ;) Where are you coding? Which IDE? does it applies any encoding of there – oetoni Nov 10 '17 at 10:15
  • Using PhpStorm, encoding is set to utf8. https://stackoverflow.com/questions/30442687/how-to-override-default-escape-function-of-blade-in-laravel-5/30443736#30443736 is for blade encoding. Here problem is DB's encoding – kumold Nov 10 '17 at 10:39
  • how is your config file (app/config/database.php)? are you sure has right encoding? sorry for asking this but because I can reproduce your error I can't assist without asking – oetoni Nov 10 '17 at 10:43
  • No problem. Added config to the question. – kumold Nov 10 '17 at 11:01
  • I will try to build a very small dev/test with this and write back - hoping to face the issue and debug – oetoni Nov 10 '17 at 11:06
  • Ok.. I've found the problem.. it was substr. Changed to mb_substr and everything is working fine.. – kumold Nov 11 '17 at 09:19

0 Answers0