Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

orm

laravel

emoji

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

like image 440
Ankit Verma Avatar asked Jul 28 '16 12:07

Ankit Verma


People also ask

How do you add emojis to laravel?

To get the latest version of Laravel Emoji, simply add the following line to the require block of your composer. json file. You'll then need to run composer install or composer update to download it and have the autoloader updated. Once Laravel Emoji is installed, you need to register the service provider.

How do I save emojis in phpmyadmin?

We will have to set our connection charset to utf8. After declaring connection variable set charset: mysqli_set_charset($con,"utf8"); After that your emoji or any other unicode text will be encrypted and saved in mysql database.

How do I support emojis in mysql?

You need to use utf8mb4 encoding in your mysql database in order to be able to store emojis.


2 Answers

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');

like image 186
djt Avatar answered Nov 07 '22 05:11

djt


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'
like image 29
Akram Wahid Avatar answered Nov 07 '22 05:11

Akram Wahid