How to store emoji string in Database using Laravel with Orm Query
Asked Answered
I

5

6

I want to store special character in database vice versa like emotive but when I try to save in database I get question marks.

Illuminator answered 28/7, 2016 at 12:45 Comment(0)
H
19

In your database.php file, make sure to set the charset and collation to utf8mb4:

'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_DATABASE', 'forge'),
            'username'  => env('DB_USERNAME', 'forge'),
            'password'  => env('DB_PASSWORD', ''),
            'charset'   => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',

And your emoji column should be a String:

$table->string('emoji');

Hedberg answered 28/7, 2016 at 13:3 Comment(5)
where is the database.php located in laravel 5?Kroll
@Webinan in the config folderHedberg
oh, I'm using Lumen and I don't have such folder named config.Kroll
@Webinan in that case, per the Lumen docs: you may use the DB_* configuration options in your .env configuration file to configure your database settings, such as the driver, host, username, and password.Hedberg
THANK YOU! DB_CHARSET + DB_COLLECTION did the job. :*Kroll
W
3

1) Ensure you're using MYSQL 5.5.3 or later then will you be able to change the collation to utf8mb4_something,

2) Ensure table columns that are going to receive emoji have their collation set to utf8mb4_something

if still you have issue, then Edit your database.php config file and update following,

    'charset' = 'utf8mb4';
'collation' = 'utf8mb4_unicode_ci'
Winnie answered 28/7, 2016 at 13:6 Comment(0)
A
2

In addition to the answers above, make sure to clear cache and migrate refresh to update the MySQL tables.

Average answered 27/1, 2017 at 3:57 Comment(0)
E
0

If you are using websocket for chat and sending Emoticons, then you have to restart the websocket.

You can find and kill all the websocket processes like sudo kill (Your PID) without the round brackets.

And for the ? Marks, it's okay because some browsers, or MySQL editors not supporting Emoticons, so they show you questions (?) marks.

Eraste answered 19/6, 2018 at 9:37 Comment(0)
S
-1

One of the best and easiest way is to change your charset to utf8mb4

charset => utf8mb4

Sieracki answered 17/8, 2022 at 3:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.