Skip to content
Advertisement

How to insert emojis in MySQL using Laravel?

My MySQL DB collation is: utf8mb4_unicode_ci As shown below: enter image description here

However when I try to insert it in the table using Laravel – it throws the following error:

JavaScript

And this is the store method in my Controller:

JavaScript

UPDATE Here is a picture of the challenges table config: enter image description here

Advertisement

Answer

This is more of a Database and migration end issue then on your laravel as you’re getting an incorrect format error. Please try:

1) Database: Change Database default collation as utf8mb4. Looks all done

2) Table: Change table collation as CHARACTER SET utf8mb4 COLLATE utf8mb4_bin.

Query:

ALTER TABLE Tablename CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin

3) Code:

Run an insert query to get an intial test of being able to handle emoji insert into tablename (columnname) values ''"

4) Set utf8mb4 in database connection:

Use raw mysql query to write the update table migration script and run php artisan migrate command

JavaScript

Note: You still have to keep the database config to utf8mb4

Hope this will help you

How to Fix the Error Code: 1833

The type and definition of the foreign key field and reference must be equal. This means your foreign key disallows changing the type of your field. What you want to do is drop the foreign key and recreate it after running the new query. You have to disallow writing to the database while you do this, otherwise, you risk data integrity problems.

JavaScript

Now run the query:

JavaScript

Recreate the foreign key:

JavaScript

I’ve added a write lock above ^

All writing queries in any other session than your own ( INSERT, UPDATE, DELETE ) will wait till timeout or UNLOCK TABLES; is executed

http://dev.mysql.com/doc/refman/5.5/en/lock-tables.html

Let me know how that goes or if you encounter any errors, I tried to match it to your case but you may need to make some adjustments to the naming conventions

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement