php 响应内容必须是实现 __toString() 的字符串或对象,移动到 psql 后给出“布尔值”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38770871/
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
The Response content must be a string or object implementing __toString(), "boolean" given after move to psql
提问by cyber8200
As soon as I move my Laravel App from MySQL to pSQL. I kept getting this error.
一旦我将我的 Laravel 应用程序从 MySQL 移动到 pSQL。我一直收到这个错误。
The Response content must be a string or object implementing __toString(), "boolean" given.
响应内容必须是实现 __toString() 的字符串或对象,给出“布尔值”。
I have an API that suppose to return my promotion
我有一个 API 可以返回我的促销
http://localhost:8888/api/promotion/1
http://localhost:8888/api/promotion/1
public function id($id){
$promotion = Promotion::find($id);
dd($promotion); //I got something here
return $promotion;
}
It used to return my promotion, now it return an error.
它曾经返回我的促销,现在它返回一个错误。
dd($promotion);
dd($促销);
I got
Promotion {#410 ▼
#table: "promotions"
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:16 [▼
"id" => 1
"cpe_mac" => "000D6721A5EE"
"name" => "qwrqwer"
"type" => "img_path"
"status" => "Active"
"heading_text" => "qwerq"
"body_text" => "werqwerqw"
"img" => stream resource @244 ?}
"img_path" => "/images/promotion/1/promotion.png"
"video_url" => ""
"video_path" => ""
"account_id" => 1001
"img_url" => ""
"footer_text" => "qwerqwerre"
"created_at" => "2016-08-04 10:53:57"
"updated_at" => "2016-08-04 10:53:59"
]
#original: array:16 [?]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [?]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
Content
内容
__ Any hints / suggestions on this will be a huge help!
__ 对此的任何提示/建议都将是一个巨大的帮助!
采纳答案by patricus
TL;DR
TL; 博士
Just returning response()->json($promotion)
won't solve the issue in this question. $promotion
is an Eloquent object, which Laravel will automatically json_encode for the response. The json encoding is failing because of the img
property, which is a PHP stream resource, and cannot be encoded.
只是返回response()->json($promotion)
并不能解决这个问题中的问题。$promotion
是一个 Eloquent 对象,Laravel 会自动为响应进行 json_encode。json 编码失败,因为img
属性是 PHP 流资源,无法编码。
Details
细节
Whatever you return from your controller, Laravel is going to attempt to convert to a string. When you return an object, the object's __toString()
magic method will be invoked to make the conversion.
无论你从控制器返回什么,Laravel 都会尝试转换为字符串。当您返回一个对象时,__toString()
将调用该对象的魔法方法来进行转换。
Therefore, when you just return $promotion
from your controller action, Laravel is going to call __toString()
on it to convert it to a string to display.
因此,当您仅从return $promotion
控制器操作时,Laravel 将调用__toString()
它以将其转换为字符串以进行显示。
On the Model
, __toString()
calls toJson()
, which returns the result of json_encode
. Therefore, json_encode
is returning false
, meaning it is running into an error.
在Model
,__toString()
调用上toJson()
,它返回 的结果json_encode
。因此,json_encode
正在返回false
,这意味着它遇到了错误。
Your dd
shows that your img
attribute is a stream resource
. json_encode
cannot encode a resource
, so this is probably causing the failure. You should add your img
attribute to the $hidden
property to remove it from the json_encode
.
您dd
表明您的img
属性是stream resource
. json_encode
无法对 a 进行编码resource
,因此这可能是导致失败的原因。您应该将您的img
属性添加到$hidden
属性以将其从json_encode
.
class Promotion extends Model
{
protected $hidden = ['img'];
// rest of class
}
回答by Jared Eitnier
Your response must return some sort of Response
object. You can't just return an object.
您的响应必须返回某种Response
对象。你不能只返回一个对象。
So change it to something like:
所以把它改成这样:
return Response::json($promotion);
or my favorite using the helper function:
或者我最喜欢的使用辅助函数:
return response()->json($promotion);
If returning a response doesn't work it may be some sort of encoding issue. See this article: The Response content must be a string or object implementing __toString(), \"boolean\" given."
如果返回响应不起作用,则可能是某种编码问题。参见这篇文章:响应内容必须是一个字符串或对象,实现了 __toString(), \"boolean\" given。
回答by Diego Alves
I got this issue when I used an ajax call to retrieve data from the database. When the controller returned the array it converted it to a boolean. The problem was that I had "invalid characters" like ú (u with accent).
当我使用 ajax 调用从数据库中检索数据时遇到了这个问题。当控制器返回数组时,它会将其转换为布尔值。问题是我有“无效字符”,比如 ú(带重音的 u)。
回答by Maniruzzaman Akash
So, rather return the whole object
first, just wrap it to json_encode
and then return it. This will return a proper and valid object.
因此,不如先返回整个object
,只需将其包装到json_encode
然后返回即可。这将返回一个正确且有效的对象。
public function id($id){
$promotion = Promotion::find($id);
return json_encode($promotion);
}
Or, For DB this will be just like,
或者,对于 DB,这就像,
public function id($id){
$promotion = DB::table('promotions')->first();
return json_encode($promotion);
}
I think it may help someone else.
我认为它可以帮助别人。
回答by pankaj kumar
You can use json_decode(Your variable Name)
:
您可以使用json_decode(Your variable Name)
:
json_decode($result)
I was getting value from Model.where a column has value like this way
我从 Model.where 中获得了价值,其中一列具有这样的价值
{"dayList":[
{"day":[1,2,3,4],"time":[{"in_time":"10:00"},{"late_time":"15:00"},{"out_time":"16:15"}]
},
{"day":[5,6,7],"time":[{"in_time":"10:00"},{"late_time":"15:00"},{"out_time":"16:15"}]}
]
}
so access this value form model. you have to use this code.
所以访问这个价值形式模型。你必须使用这个代码。
$dayTimeListObject = json_decode($settingAttendance->bio_attendance_day_time,1);
foreach ( $dayTimeListObject['dayList'] as $dayListArr)
{
foreach ( $dayListArr['day'] as $dayIndex)
{
if( $dayIndex == Date('w',strtotime('2020-02-11')))
{
$dayTimeList= $dayListArr['time'];
}
}
}
return $dayTimeList[2]['out_time'] ;
You can also define castein your Model file.
您还可以在模型文件中定义种姓。
protected $casts = [
'your-column-name' => 'json'
];
so after this no need of this line .
所以在这之后就不需要这条线了。
$dayTimeListObject = json_decode($settingAttendance->bio_attendance_day_time,1);
you can directly access this code.
您可以直接访问此代码。
$settingAttendance->bio_attendance_day_time
回答by Hemant Kumar
It is being pointed out not directly in the file which is caused the error. But it is actually triggered in a controller file. It happens when a return value from a method defined inside in a controller file is set on a boolean value. It must not be set on a boolean type but on the other hand, it must be set or given a value of a string type. It can be shown as follows :
它不是直接在导致错误的文件中指出的。但它实际上是在控制器文件中触发的。当控制器文件中定义的方法的返回值设置为布尔值时,就会发生这种情况。它不能设置为布尔类型,但另一方面,它必须设置或赋予字符串类型的值。它可以显示如下:
public function saveFormSummary(Request $request) {
...
$status = true;
return $status;
}
Given the return value of a boolean type above in a method, to be able to solve the problem to handle the error specified. Just change the type of the return value into a string type
在一个方法中给定上面一个boolean类型的返回值,就能够解决问题来处理指定的错误。只需将返回值的类型更改为字符串类型即可
as follows :
如下 :
public function saveFormSummary(Request $request) {
...
$status = "true";
return $status;
}