Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel UTF-8 To Database

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

enter image description here

And this is my database in phpmyadmin

enter image description here

Thanks

like image 481
Miguel Stevens Avatar asked Oct 13 '14 15:10

Miguel Stevens


People also ask

How do I set MySQL database to UTF-8?

To change the character set encoding to UTF-8 for the database itself, type the following command at the mysql> prompt. Replace dbname with the database name: Copy ALTER DATABASE dbname CHARACTER SET utf8 COLLATE utf8_general_ci; To exit the mysql program, type \q at the mysql> prompt.

What is UTF-8 in MySQL?

MySQL supports multiple Unicode character sets: utf8mb4 : A UTF-8 encoding of the Unicode character set using one to four bytes per character. utf8mb3 : A UTF-8 encoding of the Unicode character set using one to three bytes per character. This character set is deprecated in MySQL 8.0, and you should use utfmb4 instead.

What is UTF-8 PHP?

Definition and Usage. The utf8_encode() function encodes an ISO-8859-1 string to UTF-8. Unicode is a universal standard, and has been developed to describe all possible characters of all languages plus a lot of symbols with one unique number for each character/symbol.


1 Answers

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.

like image 71
Marcin Nabiałek Avatar answered Sep 21 '22 11:09

Marcin Nabiałek