13

When I dump the property of my model and it has an accent it returns with this "b" prefix

dump($venda_item->produto->nomeproduto); // b"teste téste"

My database is setted to utf8 and utf8_general_ci collation

This causes me the following error Malformed UTF-8 characters, possibly incorrectly encoded when I'm returning the response in json, at this line

$json_response = Response::json($response, $this->getStatusCode(), $headers);

Update

I discovered that if I die and dump the record on the web route it shows normal teste téste

Route::get('/', function () {
    dd(App\Vendasitem::where('codigovi', 112685)->first()->produto->nomeproduto);
}

otherwise if I do the same in the controller or request and other files that I tryied it keeps returning me with the "b" prefix

Update 2

If I save my record like this PROMO - VIRICAPS (GUARANá + POLIVIT) 60 CáPS - CAIXA 18 UND and dump($venda_item->produto->nomeproduto); it returns me the right result with the accents.

All my database, including the column is set up to utf8mb4 and utf8mb4_unicode_ci

27
  • So whats your question?
    – tkausl
    Commented Feb 13, 2019 at 17:02
  • Why is it with a prefix b, it broks my json, gives me a malformed utf8
    – SpaceDogCS
    Commented Feb 13, 2019 at 17:03
  • 3
    Given the way your data was corrupted when you tried @jfreites's non-answer it looks like you're still getting the data in a single-byte encoding. You should double-check that you're setting the correct charset when you set up your DB connection. Also, mysql tends to not care if you put not-UTF8 into a table with UTF8 as the declared encoding and will give you that nonsense straight back. Check your source data. stackoverflow.com/questions/279170/utf-8-all-the-way-through
    – Sammitch
    Commented Feb 13, 2019 at 17:48
  • 4
    For reference: b'...' are binary strings, introduced in 5.2 but deprecated by PHP 7. That they cause problems with JSON is known. There's no actual question in your post, that I can see. Can you clarify what you're wanting to know?
    – bishop
    Commented Feb 15, 2019 at 17:44
  • 1
    @SpaceDogCS Your answers are confusing. You should have utf8mb4 always and there won't be such problems.
    – Styx
    Commented Feb 22, 2019 at 16:33

4 Answers 4

3

If you already confirmed the encoding on your database. Have a look on config/database.php, on charset and collation proprieties.

'charset'   => 'utf8',
'collation' => 'utf8_unicode_ci'

If the issue is on the "print" then you might use the utf8_encode function.

{{utf8_encode($yourVar)}}
0

I had this problem when importing data from a different database. Solved with this method, maybe it works for you to.

public function fixEncoding($str) {
        return mb_check_encoding($str, 'UTF-8') 
                ? html_entity_decode($str) 
                : utf8_encode(html_entity_decode($str));
}
-1

So, it looks like your best bet is to write a accessor for the object using iconv:

public function getNomeprodutoAttribute($value) {
   return iconv("utf-8", "utf-8//ignore", $value);
}

Unless you want to recode the whole database, which probably isn't an option.

The value passed in will the value out of the database (which is the unfortunate binary data). Then you are telling it the input is utf (which it is), and that the output is utf8 (which we want) but to ignore chars that aren't supported in utf-8. //ignore may work in some versions of the library, so you'd just have to do the following, and hope for the best:

public function getNomeprodutoAttribute($value) {
   return iconv("utf-8", "utf-8", $value);
} 
1
  • Thank you for helping but, two problens, first, I'll need to do this to every column that I have, second it gives me an error iconv(): Detected an illegal character in input string
    – SpaceDogCS
    Commented Feb 22, 2019 at 16:33
-2

Use this function

public function utf8ize($value) {
        if (is_array($value)) {
            foreach ($value as $k => $v) {
                $value[$k] = utf8ize($v);
            }
        } else if (is_string ($value)) {
            return utf8_encode($value);
        }
        return $value;
    }
5
  • Can you explain what it does? Commented Aug 15, 2019 at 10:18
  • please check this - hotexamples.com/examples/-/-/utf8ize/…
    – Hamelraj
    Commented Aug 15, 2019 at 10:27
  • if sting is not UTF-8 format then we have to encode using utf8_encode()
    – Hamelraj
    Commented Aug 15, 2019 at 10:29
  • @cbaconnier do u know the answer ? if know post your answer
    – Hamelraj
    Commented Aug 15, 2019 at 10:52
  • I do not have an answer. My assumptions is that @SpaceDogCS mixed encoding between Laravel and the database. (IIRC file encoding is also a thing on Windows) Commented Aug 15, 2019 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.