删除 json 响应中的数字 ( LARAVEL 9 )
Getting rid of numbers in a json response ( LARAVEL 9 )
我的 json 回复有些问题。
当我从用户 (id:1) 发出 api 呼叫时,它 returns 响应很好,但是如果我更改用户 (f.e id:2) 它 returns 它上面有一些标识符。有什么想法吗?
来自id:1
的回应
{
"data": [
{
"id": 1,
"matricula": "222555999C",
"capacidad": "220",
"created_at": "2022-04-06T09:52:39.000000Z",
"updated_at": "2022-04-06T09:52:39.000000Z",
"deleted_at": null,
"transportista": 1
}
]
}
来自 id:2
的回复
{
"data": {
"3"(<--- this is the problem): {
"id": 9,
"matricula": "HYK 2024",
"capacidad": "100",
"created_at": "2022-04-08T15:12:22.000000Z",
"updated_at": "2022-04-08T15:12:22.000000Z",
"deleted_at": null,
"transportista": 2
}
}
}
谢谢!
编辑 1
这就是我得到回复的方式:
public function getCamiones(Request $request) {
$id = $request->get('id');
$camiones = Camiones::all()->where('transportista',$id);
return response()->json([
'data' => $camiones,
]);
}
现在,您将获得所有结果,然后过滤返回的集合,而不是让数据库为您完成工作。相反,只抓取一行。
$camiones = Camiones::where('transportista', $id)->first();
要获取所有匹配的行,则需要
$camiones = Camiones::where('transportista', $id)->get();
我的 json 回复有些问题。
当我从用户 (id:1) 发出 api 呼叫时,它 returns 响应很好,但是如果我更改用户 (f.e id:2) 它 returns 它上面有一些标识符。有什么想法吗?
来自id:1
的回应{
"data": [
{
"id": 1,
"matricula": "222555999C",
"capacidad": "220",
"created_at": "2022-04-06T09:52:39.000000Z",
"updated_at": "2022-04-06T09:52:39.000000Z",
"deleted_at": null,
"transportista": 1
}
]
}
来自 id:2
的回复{
"data": {
"3"(<--- this is the problem): {
"id": 9,
"matricula": "HYK 2024",
"capacidad": "100",
"created_at": "2022-04-08T15:12:22.000000Z",
"updated_at": "2022-04-08T15:12:22.000000Z",
"deleted_at": null,
"transportista": 2
}
}
}
谢谢!
编辑 1
这就是我得到回复的方式:
public function getCamiones(Request $request) {
$id = $request->get('id');
$camiones = Camiones::all()->where('transportista',$id);
return response()->json([
'data' => $camiones,
]);
}
现在,您将获得所有结果,然后过滤返回的集合,而不是让数据库为您完成工作。相反,只抓取一行。
$camiones = Camiones::where('transportista', $id)->first();
要获取所有匹配的行,则需要
$camiones = Camiones::where('transportista', $id)->get();