Laravel UTF-8 转数据库

2021-12-26 00:00:00 php laravel eloquent

我正在使用 Eloquent 将一个新人保存()到我的数据库中.人名包含一个特殊字符 é 并且它不是提交.这是我的步骤和结果.

I'm using Eloquent to save() a new person into my database. The persons name contains a special character é and it's not submitting. Here are my steps and the results.

echo Input::get('firstname'); // Miguél

这给了我这个

当我开始使用 eloquent 时,会发生以下情况.

When i start using eloquent the following happens.

$person = new Person();
echo $person->firstname = Input::get('firstname'); 

结果如下

知道可能出了什么问题吗?这些是我在 laravel 中的配置设置

Any idea what might be going wrong? These are my config settings in laravel

这是我在 phpmyadmin 中的数据库

And this is my database in phpmyadmin

谢谢

推荐答案

我认为它与数据库没有任何共同之处.

I don't think it has anything common with database.

使用时:

$person = new Person();
echo $person->firstname = Input::get('firstname'); 

你在这里不使用数据库.您只需将属性分配给 Person 类(可能使用 Eloquent),但您没有将任何内容放入数据库并从数据库中获取任何内容,因此编码问题不可能与数据库本身有任何共同之处

you don't use database in here. You just assign properties to Person class (that probably uses Eloquent) but you don't put anything into database and get anything from database so it's not possible that the encoding problem has anything in common with database itself

我认为潜在的问题 - 您已经在 Person 类中为 firstname 属性定义了mutator,因为它是小写的(当你从 Input 中得到它时,它是大写的) 所以你可能使用了一些像 strtolower 这样的函数,你应该使用 mb_strtolower 来转换 UTF-8 字符串而不会有问题.

Potential problem in my opinion - you have defined mutator in Person class for firstname attribute because you have it in lowercase (when you get it from Input it's with capital letter) so you probably use some function like strtolower and you should use mb_strtolower to convert UTF-8 strings without a problem.

相关文章