Emojis, Laravel and MySQL 🤔🦄👍
If you are getting "SQLSTATE[HY000]: General error: 1366 Incorrect string value" error when trying to insert a Emoji into your MySQL database with Laravel. Here's the solution!
The problem is that Laravel by default are using utf8 as charset. In MySQL an UTF8 char is up to 3 bytes long (“utf8” is an alias for utf8mb3). Emoji characters are up to 4 bytes long. Therefor we need to use “utf8mb4” as our charset. Luckly there is an easy fix.
Find the mysql section:
'mysql' => [ 'driver' => 'mysql', [...] 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', [....]
Change charset collation to desired value. Probably “utf8mb4” and “utf8mb4_unicode_ci” if you are creating an app for english speaking people:
'mysql' => [ 'driver' => 'mysql', [...] 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', [....]
Save and reset your database, done! Head’s up! If you reset your database all your data will be erased! 👹
> php artisan migrate:reset