kyo*_*kyo 24 php mysql laravel psql laravel-5
只要我将Laravel应用程序从MySQL移动到pSQL.我一直收到这个错误.
Response内容必须是实现__toString()的字符串或对象,给出"boolean".
我有一个API,假设要退回我的促销活动
public function id($id){
$promotion = Promotion::find($id);
dd($promotion); //I got something here
return $promotion;
}
Run Code Online (Sandbox Code Playgroud)
它曾用于返回我的促销,现在它返回错误.
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
}
Run Code Online (Sandbox Code Playgroud)
__任何关于此的提示/建议都将是一个巨大的帮助!
Jar*_*ier 26
您的回复必须返回某种Response
对象.你不能只返回一个对象.
所以将它改为:
return Response::json($promotion);
Run Code Online (Sandbox Code Playgroud)
或者我最喜欢使用辅助函数:
return response()->json($promotion);
Run Code Online (Sandbox Code Playgroud)
如果返回响应不起作用,则可能是某种编码问题.请参阅此文章:Response内容必须是实现__toString()的字符串或对象,给出"boolean"."
pat*_*cus 17
当你刚从response()->json($promotion)
控制器动作开始时,Laravel将调用$promotion
它将其转换为要显示的字符串.
在img
,__toString()
调用return $promotion
,返回结果__toString()
.因此,Model
正在返回__toString()
,这意味着它正在遇到错误.
您的toJson()
节目表明您的json_encode
属性是json_encode
.false
不能编码dd
,所以这可能导致失败.您应该将img
属性添加到stream resource
属性以将其从中删除json_encode
.
class Promotion extends Model
{
protected $hidden = ['img'];
// rest of class
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
77846 次 |
最近记录: |