Laravel 5.1 utf-8 保存到数据库

Laravel 5.1 utf-8 saving to database

我正在尝试将记录保存到数据库。 从输入中获取值并将其保存到数据库时没有问题,例如:

$request->input('name') is an input with value of 'سلام'

$provider->name = $request->input('name');
$provider->copyright_email = 'test@yahoo.com';
$provider->save();

但是当我尝试从我的控制器中获取值时出现问题。 名称将保存 '?'进入数据库:

$provider->name = 'سلام';
$provider->copyright_email = 'test@yahoo.com';
$provider->save();

我已经将此代码添加到 config/database。php :

'charset' => 'utf8',
'collation' => 'utf8_persian_ci',
  1. 将您的数据库文件配置为 utf8_unicode_ci

检查文件 config/database.php :

'charset'   => 'utf8',
'collation' => 'utf8_unicode_ci',
  1. 确保您的 Mysql 数据库设置为 utf8 并且 MySQL 的 utf8mb4 更好

  2. 确保你的文件字符设置为UTF-8 without BOM

我个人认为你的问题在你IDE,尝试使用Atom。

将您的数据库文件配置为 utf8mb4_unicode_ci

检查文件 config/database.php :

'charset' => 'utf8', 'collation' => 'utf8mb4_unicode_ci',

我遇到了同样的问题,接受的答案对我没有帮助。 我是这样解决问题的:

转到数据库中的 table,检查 'Collation',将其更改为:

utf8mb4_unicode_ci  

将此应用于 table 中的每个字段。

这可能对某些人有所帮助。 :)

你也可以使用下面的代码设置utf8 ->字符集('utf8')

我的问题 使用

'charset'   => 'utf8',
'collation' => 'utf8_unicode_ci',

无效

但用于迁移

$table->charset = 'utf8';
$table->collation = 'utf8_persian_ci';