响应内容必须是实现__toString(),"boolean"的字符串或对象.移至psql后给出 [英] The Response content must be a string or object implementing __toString(), "boolean" given after move to psql
问题描述
我将Laravel App从MySQL迁移到pSQL之后.我一直收到此错误.
As soon as I move my Laravel App from MySQL to pSQL. I kept getting this error.
Response内容必须是实现__toString()的字符串或对象,并给出"boolean".
The Response content must be a string or object implementing __toString(), "boolean" given.
我有一个API可以返回我的促销信息
I have an API that suppose to return my promotion
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);
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!
推荐答案
TL; DR
仅返回response()->json($promotion)
不会解决此问题中的问题. $promotion
是一个雄辩的对象,Laravel将自动使用json_encode进行响应. json编码由于img
属性而失败,该属性是PHP流资源,无法进行编码.
TL;DR
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.
无论您从控制器返回什么,Laravel都将尝试转换为字符串.返回对象时,将调用该对象的__toString()
魔术方法进行转换.
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.
因此,当您只是从控制器操作中按return $promotion
时,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.
在Model
上,__toString()
调用toJson()
,这将返回json_encode
的结果.因此,json_encode
返回false
,表示它遇到错误.
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.
您的dd
显示您的img
属性是stream resource
. json_encode
无法对resource
进行编码,因此这可能导致失败.您应该将img
属性添加到$hidden
属性中,以将其从json_encode
中删除.
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
.
class Promotion extends Model
{
protected $hidden = ['img'];
// rest of class
}
这篇关于响应内容必须是实现__toString(),"boolean"的字符串或对象.移至psql后给出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!