laravel 拉拉维尔。响应内容必须是实现 __toString() 的字符串或对象,给出“布尔值”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35957157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:24:11  来源:igfitidea点击:

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

phplaravellaravel-5

提问by nirvair

This is my array.

这是我的数组。

http://pastebin.com/eTrJ2PVB

When I return this array like this, I get an error.

当我像这样返回这个数组时,我收到一个错误。

return \Response::json($response, 200, [], JSON_NUMERIC_CHECK);

However, when I remove JSON_NUMERIC_CHECK, I get the response; but the problem is I need numeric values.

但是,当我删除 JSON_NUMERIC_CHECK 时,我得到了响应;但问题是我需要数值。

What could be done to resolve this?

可以做些什么来解决这个问题?

回答by Malta

You don't mention what error you are getting, but I can't reproduce any errors either. In fact this code at http://pastebin.com/GJWrCgwNshows your array returned as a perfectly formatted JSON with literal numbers instead of strings - see results at http://pastebin.com/cYDzDGyE.

您没有提到您遇到的错误,但我也无法重现任何错误。事实上,http://pastebin.com/GJWrCgwN 上的这段代码显示您的数组以完美格式化的 JSON 形式返回,其中包含文字数字而不是字符串 - 请参阅http://pastebin.com/cYDzDGyE 上的结果。

BTW, you can also use this alternative syntax where you don't need to mess with HTTP response status code or headers, just the JSON encoding options:

顺便说一句,您也可以使用这种替代语法,您不需要弄乱 HTTP 响应状态代码或标头,只需使用 JSON 编码选项:

return response()->json($response)->setEncodingOptions(JSON_NUMERIC_CHECK);

Check it out at Symfony\Component\HttpFoundation\JsonResponse.

Symfony\Component\HttpFoundation\JsonResponse.