Rikhi Sahu
Rikhi Sahu

Reputation: 655

Laravel : How can I store Hindi Character in MySql Database

I want to store Hindi character in mysql database using laravel application

I have below properties with database

 Database charset = utf8
 Database collation = utf8_general_ci

and these setting with config/database.php

 'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'db'),
        'username' => env('DB_USERNAME', 'user'),
        'password' => env('DB_PASSWORD', 'password'),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8',
        'collation' => 'utf8_general_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

but when i try to store data it gives me this error

"message": "SQLSTATE[HY000]: General error: 1267 Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8mb4_general_ci,COERCIBLE) for operation '=' (SQL: select * from `col` where (`name` = \u091b\u0924\u094d\u0924\u094b\u0917) limit 1)",
"exception": "Illuminate\\Database\\QueryException",

please suggest me something, i don't understand what i am missing

Upvotes: 0

Views: 487

Answers (2)

Rikhi Sahu
Rikhi Sahu

Reputation: 655

I was setting Charset utf8 and Collation utf8_general_ci for Database and Table but we need to check if table columns have the same Charset and Collation or not, i was missing the same or i didn't knew about that :)

enter image description here

Upvotes: 0

Rick James
Rick James

Reputation: 142278

As hinted in the error message, latin1 is showing up somewhere. It is probably the default setting for the client.

Your config/database.php looks OK. So let's tackle it a different way:

Right after connecting, perform the SQL command SET NAMES utf8.

I can't tell where \u091b\u0924\u094d\u0924\u094b\u0917 is coming from, but you should really use UTF-8 encoding in your client, not that Unicode equivalent.

Upvotes: 1

Related Questions