Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use and save Emoji's on Laravel using Mysql

I have a web app I built using Laravel 5.2 , my question is how do I store then show emoji's. At the moment when I use an emoji from my iPhone , fire for instance, after saving the database returns ???? . Ive set the 'charset' = 'utf8mb4'; 'collation' = 'utf8mb4_unicode_ci'

but still get the same result. Do i need to install a package or something?

like image 446
Luna Avatar asked Dec 21 '16 05:12

Luna


People also ask

Can you save Emojis in MySQL?

Some emojis such as ⚡ can be stored in MySQL's 3-byte utf8 encoding, but others such as face screaming in fear cannot.

How do I save Emojis to my database?

Use urlEncoder to encode your String having emoticons. Store it in DB without altering the MysqlDB. You can store it in solr core(decoded form)if you want or you can store encoded form.


1 Answers

Make sure you have the charset and collation correctly setup in the database config file as well, to ensure Laravel actually knows what charset to use when saving:

// config/database.php

'mysql' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', 'localhost'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_general_ci',
    'prefix' => '',
    'strict' => false,
    'engine' => null,
],
like image 83
Tomas Buteler Avatar answered Oct 20 '22 00:10

Tomas Buteler