Did you try to return the plain array? Laravel should transform it into JSON automagically.
$address = ['şâţ' => 1];
return $address;
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello, I`m playing with some utf-8 strings in a ajaxcontroller and i get an error: The Response content must be a string or object implementing __toString(), "boolean" given
$address = ['şâţ' => 1];
$response = response()->json($address);
$response->header('Content-Type', 'application/json');
$response->header('charset', 'utf-8');
return $response;
Anyone know a fix for that? Thanks!
Did you try to return the plain array? Laravel should transform it into JSON automagically.
$address = ['şâţ' => 1];
return $address;
I tryed and get same error.
UnexpectedValueException in Response.php line 403: The Response content must be a string or object implementing __toString(), "boolean" given.
in Response.php line 403
at Response->setContent(false) in Response.php line 47
at Response->setContent(array('���' => '1')) in Response.php line 202
at Response->__construct(array('���' => '1')) in Router.php line 1188
at Router->prepareResponse(object(Request), array('���' => '1')) in Router.php line 692
@oriceon When you return response json in Laravel as far as i remember it sets headers for you. So you don`t need to repeat that.
Did this code works when you just put 'foo' in your keyname instead of that jibrish?
@Kemito Yes it is workin if i had
$address = ['foo' => 1];
As i see it is a utf-8 problem. I tryed code with and without header. I know that laravel sets by default headers..
@oriceon Are you using latest version of laravel? (currently 5.0.21)
https://ziedlejas.lv/screenshots/img.2015.03.27.ncvy9r.png
Here is screenshot of fresh instalation on Laravel.
Optionaly from 4.1 version (i guess nothing changes for that in 5.0.*) you can pass 4th param to escape utf-8 characters.
return response()->json(['şâţ' => 1], 200, [], JSON_UNESCAPED_UNICODE);
More json_encode (on what is based json response) codes please see http://php.net/manual/en/function.json-encode.php
@Kemito i`m using 5.0.20 (21 has some bugs). I guess code is workin cause is supposed to. Is not a complicated stuff :)
I tested now on a fresh installation and is workin. But dono why in my laravel project is not doing well.
May be related to this or not, but a workaround for me was to change the configuration of the FreeTDS driver that I'm using to access the sql server from linux., in /etc/freetds.conf change the version and set the UTF-8 encoding:
[global]
# TDS protocol version
tds version = 4.1
,to:
[global]
# TDS protocol version
tds version = 7.2
client charset = UTF-8
1.- create the following function:
function utf8_encode_deep(&$input) { if (is_string($input)) { $input = utf8_encode($input); } else if (is_array($input)) { foreach ($input as &$value) { self::utf8_encode_deep($value); }
unset($value);
} else if (is_object($input)) {
$vars = array_keys(get_object_vars($input));
foreach ($vars as $var) {
self::utf8_encode_deep($input->$var);
}
}
}
2.- utf8_encode_deep($address);
3.- return response()->json($address);
Samples;
$address = ['şâţ' => 1];
$responsecode = 200;
$header = array (
'Content-Type' => 'application/json; charset=UTF-8',
'charset' => 'utf-8'
);
return response()->json($address , $responsecode, $header, JSON_UNESCAPED_UNICODE);
```
use dd() instead of return
this also may happen if you have geometry fields
You can edit below function in "Illuminate\Http\JsonResponse.php"
public function __construct($data = null, $status = 200, $headers = [], $options = 0)
{
$headers = ['Content-Type' => 'application/json; charset=UTF-8',
'charset' => 'utf-8'];
$options = JSON_UNESCAPED_UNICODE;
$this->encodingOptions = $options;
parent::__construct($data, $status, $headers);
}
you can easily get the response in 'UTF-8' by using given function.just you have t pass the array of your response in given function
public function utf8_converter($array)
{
array_walk_recursive($array, function(&$item, $key){
//if(!mb_detect_dcoding($item, 'utf-8', true)){
$item = utf8_decode($item);
//}
});
return $array;
}
I hope it is helpful
Please or to participate in this conversation.