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

Community
  • 1
  • 1
SpaceDogCS
  • 2,808
  • 3
  • 20
  • 49
  • So whats your question? – tkausl Feb 13 '19 at 17:02
  • Why is it with a prefix b, it broks my json, gives me a malformed utf8 – SpaceDogCS Feb 13 '19 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. https://stackoverflow.com/questions/279170/utf-8-all-the-way-through – Sammitch Feb 13 '19 at 17:48
  • @Sammitch I can't figure out, in laravel, my database settings is http://prntscr.com/mkoqaw using Navicat I see that my table is with the same following configuration http://prntscr.com/mkoqti same with the current table http://prntscr.com/mkor23 and I alredy update the field manually to fix any copy and paste with incorrect encoding, what else do I need to check? – SpaceDogCS Feb 13 '19 at 18:14
  • @Sammitch another print of the database http://prntscr.com/mkorst – SpaceDogCS Feb 13 '19 at 18:15
  • 4
    For reference: `b'...'` are binary strings, introduced in [5.2](https://web.archive.org/web/20090403051843/http://us2.php.net/manual/en/function.is-binary.php) but deprecated by PHP 7. That they cause problems with JSON [is known](https://stackoverflow.com/a/36273464/2908724). There's no actual question in your post, that I can see. Can you clarify what you're wanting to know? – bishop Feb 15 '19 at 17:44
  • @bishop the string shouldn't be a binary string, thats the problem, it is just a string with an accent, I want to know wat is causing it to become a binary string, debuging laravels framkework, i've notice that it becomes a binary string in some part between a pipeline and the authentication middleware how you can see in my another question here https://stackoverflow.com/questions/54708267/getting-binary-data-after-pipeline – SpaceDogCS Feb 15 '19 at 17:49
  • @bishop I'm using PHP 7 so this string shouldn't show up right? – SpaceDogCS Feb 15 '19 at 17:53
  • What type has this `nomeproduto` column? – Styx Feb 15 '19 at 20:36
  • @Styx varchar(255) – SpaceDogCS Feb 15 '19 at 20:41
  • Show also your `Vendasitem` model, please. – Styx Feb 16 '19 at 07:21
  • Are you sure that the column has the same collation as the table? You can check it with request like SHOW FULL COLUMNS FROM produto WHERE Field = 'nomeproduto' – Andrii Filenko Feb 18 '19 at 19:47
  • @AndriiFilenko Yes, I've alredy checked that too http://prntscr.com/mmuq2r – SpaceDogCS Feb 18 '19 at 19:52
  • Your second update is somewhat messed up at the end. – Styx Feb 18 '19 at 20:51
  • @Styx this hole problem is a mess kkk but I simplified, take a look – SpaceDogCS Feb 18 '19 at 20:58
  • Yes, it is. I was sure your DB had `utf8mb4` from the start, seems like I've read the question inattentively :) You shouldn't have problems with `utf8mb4` in future. – Styx Feb 18 '19 at 21:12
  • @Styx before it was only in `utf8`, but I still with problem, cause in my database it should be with the correct caracter not that weird one – SpaceDogCS Feb 19 '19 at 13:19
  • The problem was using `utf8` encoding for database. It isn't _real_ unicode actually. `utf8mb4` however, is. – Styx Feb 19 '19 at 13:43
  • Did you try `utf8_encode($venda_item->produto->nomeproduto)` before transforming to json? – Gli Feb 21 '19 at 17:27
  • @GlaucoA.Amigo that inverts my error on web route it will show the wrong string and in the controllers it will be okay, I have more pages that I show that same field and I can't convert every field that I have cause it is hundreds – SpaceDogCS Feb 21 '19 at 17:32
  • @SpaceDogCS Hold on, are still having this problem? – Styx Feb 21 '19 at 22:04
  • @Styx Yes if I put an accent on the database, it still returning binary, if I use this encode `á` that I don't know the name, it shows the accent normal – SpaceDogCS Feb 22 '19 at 12:48
  • @SpaceDogCS How do you "put an accent into database"? Though usual methods in your code? – Styx Feb 22 '19 at 13:57
  • @Styx I've made severals projects with accents, never changing anything on my configuration, here you can see an example http://prntscr.com/moq5ev I just save the value of an html form input, but if I open my table and manualy put an phrase with accent works too – SpaceDogCS Feb 22 '19 at 16:21
  • @Styx always in utf8 and utf8_general_ci – SpaceDogCS Feb 22 '19 at 16:23
  • 1
    @SpaceDogCS Your answers are confusing. You should have `utf8mb4` always and there won't be such problems. – Styx Feb 22 '19 at 16:33
  • @Styx yes, but in all my projects I always have utf8 and never had this problem, now in this project I have this problem with both utf8 and utf8mb4 – SpaceDogCS Feb 22 '19 at 16:34

4 Answers4

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)}}
Tiago Mateus
  • 356
  • 4
  • 17
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));
}
Thiago Brauer
  • 321
  • 4
  • 9
-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);
} 
J. A. Streich
  • 1,683
  • 10
  • 13
  • 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 Feb 22 '19 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;
    }
Hamelraj
  • 4,676
  • 4
  • 19
  • 42