一旦我将Laravel App从MySQL迁移到pSQL。我一直收到这个错误。
Response内容必须是实现__toString()的字符串或对象,给定为“ boolean”。
我有一个API可以返回我的促销
http:// localhost:8888 / api / promotion / 1
public function id($id){
$promotion = Promotion::find($id);
dd($promotion); //I got something here
return $promotion;
}
它曾经返回我的促销,现在返回一个错误。
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
}
内容
__关于此的任何提示/建议都会有很大帮助!
TL; DR
仅仅回来response()->json($promotion)
就不能解决这个问题。$promotion
是一个雄辩的对象,Laravel会自动将json_encode用作响应。json编码由于该img
属性而失败,该属性是PHP流资源,无法进行编码。
细节
无论您从控制器返回什么,Laravel都将尝试转换为字符串。当您返回对象时,__toString()
将调用该对象的magic方法进行转换。
因此,当您仅从return $promotion
控制器操作开始时,Laravel便会调用__toString()
它以将其转换为要显示的字符串。
在上Model
,__toString()
调用toJson()
,返回的结果json_encode
。因此,json_encode
正在返回false
,表示它遇到了错误。
您dd
显示您的img
属性是stream resource
。json_encode
无法编码resource
,因此很可能导致失败。您应该将img
属性添加到$hidden
属性中,以将其从中删除json_encode
。
class Promotion extends Model
{
protected $hidden = ['img'];
// rest of class
}
您的响应必须返回某种Response
对象。您不能只返回一个对象。
因此,将其更改为:
return Response::json($promotion);
或我最喜欢使用辅助功能:
return response()->json($promotion);
如果返回响应不起作用,则可能是某种编码问题。请参阅本文:Response内容必须是实现__toString()的字符串或对象,已指定\“ boolean \”。
当我使用ajax调用从数据库检索数据时遇到了这个问题。当控制器返回数组时,它将其转换为布尔值。问题是我有诸如ú(带有重音符号的u)的“无效字符”。
因此,宁愿object
先将其整体退货,也要先将其包装json_encode
然后再返还。这将返回一个正确且有效的对象。
public function id($id){
$promotion = Promotion::find($id);
return json_encode($promotion);
}
或者,对于DB,这就像
public function id($id){
$promotion = DB::table('promotions')->first();
return json_encode($promotion);
}
我认为这可能会帮助其他人。
指出不是直接在导致错误的文件中指出的。但是它实际上是在控制器文件中触发的。当将控制器文件内部定义的方法的返回值设置为布尔值时,就会发生这种情况。不能将其设置为布尔类型,但另一方面,必须将其设置或赋予字符串类型的值。它可以显示如下:
public function saveFormSummary(Request $request) {
...
$status = true;
return $status;
}
给定一个方法中上面布尔类型的返回值,以便能够解决该问题以处理指定的错误。只需将返回值的类型更改为字符串类型
如下 :
public function saveFormSummary(Request $request) {
...
$status = "true";
return $status;
}
您可以使用json_decode(Your variable Name)
:
json_decode($result)
我从Model。那里获得价值,其中一列具有这样的价值
{"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"}]}
]
}
因此,请访问此值表单模型。您必须使用此代码。
$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'] ;
您也可以在模型文件中定义种姓。
protected $casts = [
'your-column-name' => 'json'
];
因此,在此之后不需要此行。
$dayTimeListObject = json_decode($settingAttendance->bio_attendance_day_time,1);
您可以直接访问此代码。
$settingAttendance->bio_attendance_day_time
评论已关闭!