Laravel UTF-8 转数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26343876/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:16:00  来源:igfitidea点击:

Laravel UTF-8 To Database

phplaraveleloquent

提问by Miguel Stevens

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.

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

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

Which gives me this

这给了我这个

Miguél

米格尔

When i start using eloquent the following happens.

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

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

This produces the following result

这会产生以下结果

migu??l

咪咕??l

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

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

enter image description here

在此处输入图片说明

And this is my database in phpmyadmin

这是我在 phpmyadmin 中的数据库

enter image description here

在此处输入图片说明

Thanks

谢谢

回答by Marcin Nabia?ek

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

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

When you use:

当您使用:

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

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 类(可能使用 Eloquent),但您没有将任何内容放入数据库并从数据库中获取任何内容,因此编码问题不可能与数据库本身有任何共同之处

Potential problem in my opinion - you have defined mutator in Personclass for firstnameattribute because you have it in lowercase (when you get it from Input it's with capital letter) so you probably use some function like strtolowerand you should use mb_strtolowerto convert UTF-8 strings without a problem.

我认为潜在的问题 - 您已经在Person类中为firstname属性定义了 mutator,因为它是小写的(当您从 Input 获取它时,它是大写字母)所以您可能使用了一些类似的函数,strtolower并且您应该使用它mb_strtolower来转换 UTF-8 字符串而不一个问题。