'Utf8 encoding issue with Laravel

I have an issue after deploying my laravel website ( that works properly in local ). Some text are not encoded correctly, for example : what should be Joël is Joël

Some information :

  • Not all views are affected, some variables are rendered properly in a view and broken on another view ( which make me think it's not a mysql issue )

  • My json response are not working anymore ( Malformed UTF-8 characters, possibly incorrectly encoded ) , which make my think it must be a mysql problem ... )

  • I already tried to put this in my AppServiceProvider \Blade::setEchoFormat('e(utf8_encode(%s))');

  • My strftime were also not working but it can be resolved with utf8_encode function of php, which is not working with my other variables. For example if $c->name gives Joël , utf8_encode($c->name) also gives Joël

  • In config/database.php, charset is set to utf8mb4 and collaction is set to utf8mb4_unicode_ci

Would you have any idea to solve my problem ?



Solution 1:[1]

Just do this in a Service Provider AppServiceProvider and put into boot method

Blade::setEchoFormat('e(utf8_encode(%s))');

Generally sticking to UTF-8 keeps life simple.

Be super careful copying and pasting from anywhere else into your code - basically always go through Notepad++ and use its convert to UTF-8 (without BOM) before copying and pasting into your code.

Then make sure all your views (including error pages), have

<meta charset="UTF-8">

Or the following if you're doing HTML4

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

Hope this will help you

Solution 2:[2]

I found a solution :

I use

{!! htmlentities($variable, ENT_QUOTES, "UTF-8") !!}

But it's not convenient ...

I tried :

Blade::setEchoFormat('e(htmlentities(%s,ENT_QUOTES,'UTF-8'))');

in AppServiceProvider but it doesn't solve the problem.

So it's a temporary solution ...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MD. Jubair Mizan
Solution 2 Pierre Ftn