Laravel UTF-8数据库 [英] Laravel UTF-8 To Database

查看:195
本文介绍了Laravel UTF-8数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

哪个给我这个



Miguél



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

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

这会产生以下结果



migu l



任何想法可能会出错?
这些是我在laravel中的配置设置





这是我在phpmyadmin中的数据库





谢谢

解决方案

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



当您使用:

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

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



我认为潜在的问题 - 您在 Person 中为 firstname 属性定义了mutator,因为你有小写(当你从Input输入它是大写字母),所以你可能使用一些函数,如 strtolower ,你应该使用 mb_strtolower 可以转换UTF-8字符串而不会出现问题。


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

Which gives me this

Miguél

When i start using eloquent the following happens.

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

This produces the following result

migu��l

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

And this is my database in phpmyadmin

Thanks

解决方案

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

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.

这篇关于Laravel UTF-8数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆