charset UTF8

Hello, I have simple application showing one table with results to authenticated users Illuminate\Foundation\Auth\AuthenticatesUsers. Until I loaded into databse real data containing special characters the data was shown correctly in the table. Now is not working the json_encode() function that I am using to convert the result of DB::table("mytable")->get() to json object. Now is giving empty string. Somewhere in the internet I found, that this might be caused by having the array with values not in UTF8 encoding. I have checked my database and columns and everywhere I see collation utf8_czech_ci. I found that in the config/database.php in the connections=>mysql is not set the encoding. So I have added it:

'connections' => [
        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', ''),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'empresa'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', '............'),
            'charset'   => 'utf8',

After adding the line 'charset' => 'utf8' the application gets an error 500 saying "Undefined index: collation (SQL: select * from users where id = 1 limit 1)" When I delete the charset in the database configuration file everything is again working fine (except of the json_encode() function.

Does anybody know what can be wrong?

Slava replied 11 months ago

You can print result of DB::table('mytable')->get() without json_encode. And analyse that.. May be some fields have something just like this: "Some fie�d dat��" or "Some fie?d dat??"

Sign in to participate in this thread!

We'd like to thank these amazing companies for supporting us