Laravel响应是String而不是int


public function create(Request $request){
$comment = new Comment;
$comment->user_id = Auth::user()->id;
$comment->post_id = $request->id; //here is where the response is string
$comment->comment = $request->comment;
$comment->save();
$comment->user;
return response()->json(
$comment,
);
}

我不知道为什么响应是字符串id,而在数据库迁移中,post_id表是$table->unsignedBigInteger('post_id');,它假设是整数。。

{
"user_id": 4,
"post_id": "2", //response is string 
"comment": "test string id finale",
"updated_at": "2020-10-31T19:55:49.000000Z",
"created_at": "2020-10-31T19:55:49.000000Z",
"id": 11,
}

转到您的Comment模型并添加:

protected $casts = [
'post_id' => 'integer',
];

字符串(总是在请求中发送(应该在模型上转换为int(然后以数字的形式出现在JSON中(。

相关内容

最新更新