如何使用 Laravel 和 Orm Query 在数据库中存储表情符号字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38636783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to store emoji string in Database using Laravel with Orm Query
提问by Ankit Verma
I want to store special char in database vice versa like emotive but when i try to save in database i get question marks.
我想在数据库中存储特殊字符,反之亦然,就像情绪一样,但是当我尝试保存在数据库中时,我得到了问号。
Please help
请帮忙
回答by djt
In your database.php
file, make sure to set the charset
and collation
to utf8mb4
:
在您的database.php
文件中,确保将charset
和设置collation
为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');
$table->string('emoji');
回答by Akram Wahid
1) Ensure you're using MYSQL 5.5.3 or later then will you be able to change the collation to utf8mb4_something,
1) 确保您使用的是 MYSQL 5.5.3 或更高版本,然后您可以将排序规则更改为 utf8mb4_something,
2) Ensure table columns that are going to receive emoji have their collation set to utf8mb4_something
2) 确保将要接收表情符号的表列的排序规则设置为 utf8mb4_something
if still you have issue, then Edit your database.php config file and update following,
如果仍有问题,请编辑您的 database.php 配置文件并更新以下内容,
'charset' = 'utf8mb4';
'collation' = 'utf8mb4_unicode_ci'
回答by hellorrr
In addition to the answers above, make sure to clear cache and migrate refresh to update the mysql tables. hope this helps someone
除了上面的答案,请确保清除缓存并迁移刷新以更新 mysql 表。希望这有助于某人
回答by Tahir Afridi
If you are using websocket for chat and sending Emoticons, then you have to restart the websocket.
如果您使用 websocket 进行聊天和发送表情符号,则必须重新启动 websocket。
You can find and kill all the websocket processes like
sudo kill (Your PID)
without the round brackets.
您可以找到并杀死所有 websocket 进程,就像
sudo kill (Your PID)
没有圆括号一样。
And for the ??? marks, its okey because some browsers, or MYSQL editors not supporting Emoticons, so they show you questions (?) marks.
而对于???标记,没关系,因为某些浏览器或 MYSQL 编辑器不支持表情符号,因此它们会向您显示问题 (?) 标记。
Regards.
问候。