Support the ongoing development of Laravel.io →
Configuration Database Blade
Last updated 2 years ago.
0

are you using mysql? check the encoding of your tables

run this command inside mysql cli

show create table table_name

at then end of the table you must see something like this:

DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

but this default setting can be overwritten by the column type, for example if you have some text column, you also must see something like this

`column_name` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
Last updated 2 years ago.
0

I think this time it's not a db issue.
I tried to get the same row from a new blank document, using mysqli and it works as expected

<html>
<head><meta charset="utf-8"></head>
<body><?php
$mysqli = new mysqli(.......);

$res = $mysqli->query('select * from the_same_table');

$row = $res->fetch_object();

echo $row->column;

?></body>
</html>

prints

NEB Fab4 2.0 ☢
Last updated 2 years ago.
0

After setting the charset on the tables maybe you should try to do:

$res = $mysqli->query('SET NAMES utf8');

before the main select query.

And the php file also must be saved in the same utf8.

Last updated 2 years ago.
0

Using mysqli works fine, it doesn't work using laravel db:: or eloquent model::!

this is the query

$res = DB::table($tableName)->select(DB::raw('*, count(col) as tot'))->groupBy('col')->get()
Last updated 2 years ago.
0

I can't get the right utf-8 decoding!
I have this query

select name from table ....

mysqli gets this result

$mysqli->query('select name from table');
....
array (size=1)
  'name' => string 'Altaïr' (length=7)
....

eloquent

User::all()
....
"name":"Alta\u00c3\u00afr"
....

printing the result of mysqli query (in a view with charset utf-8)

Altaïr <--- as expected

printing the result of eloquent query

Altaïr

What am I doing wrong?

Last updated 2 years ago.
0

I am also trying to do something like this

$names = DB::table($tableName)->groupBy('uid')->get(array('name'));
        foreach($names as $user) {
            $user->name = utf8_decode($user->name);
        }
        return $names;

and ...

UnexpectedValueException
The Response content must be a string or object implementing __toString(), "boolean" given.

if I do the same thing less the utf8_decode

$names = DB::table($tableName)->groupBy('uid')->get(array('name'));
        foreach($names as $user) {
            $user->name = $user->name . '_modified';
        }
        return $names;

works fine

[{"name":"John_modified"},{"name":"Alta\u00c3\u00afr_modified"},

and, one other thing, if I do

$names = DB::table($tableName)->groupBy('uid')->get(array('name'));
        foreach($names as $user) {
            var_dump($user);
            $user->name = utf8_decode($user->name);
            var_dump($user);
        }
        die;
        return $names;

I get this

....
object(stdClass)[532]
  public 'name' => string 'Altaïr' (length=9)
object(stdClass)[532]
  public 'name' => string 'Altaïr' (length=7)
....

I don't know what else I can do....

Last updated 2 years ago.
0

Hi

I was also facing same issues. after debugging laravel "MySqlConnector" class

i have just commented this line $connection->prepare($names)->execute(); [line 38] and its start working.

The problem with our database from start its not utf8 then this issues is coming.

0

Are you doing this on the command line or in a view?

IF a view: Does your UTF-8 show up properly in HTML?

IF command line: Does your command line support the language your are trying to print_out to?

I work a lot with Japanese and have no issues if my Database is set to: Type: InnoDB Encodign: UTF-8 Unicode (utf8) Collation: utf8_unicode_ci

and my HTML has: <meta charset="utf-8">

I haven't worried about doing anything to my queries in years ...

0

In my case, I read from a third party Data source and there are some illegal characters mixed in the result.

It's useful that use utf8_decode function to avoid this error.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.