Laravel Group By 响应格式不一致
Laravel Group By Inconsistent response format
目前我正在开发一个员工管理系统,该系统具有存储用户目标的功能。
每次返回数据时,根据哪个用户,数据JSON格式不一致,因此无法在前端正常显示。
控制器代码是
public function index(Request $request){
$user = $request->user();
if($request->query('user')){
$user = User::find($request->query('user'));
$goals = $user->goals->groupBy('status');
return response()->json($goals, 200);
}
$goals = $user->goals->groupBy('status');
if(!$goals){
return response()->json([
"Message" => "No goals found."
], 404);
}
return response()->json($goals, 200);
}
在用户模型上定义了目标关系:
public function goals(){
return $this->hasMany(Goal::class, 'user_id');
}
当我请求目标 ID 为 25s 的用户时,我得到了这个响应(我想要的格式)。
[
[
{
"id": 22,
"user_id": 25,
"title": "Quia quibusdam nisi aperiam harum.",
"description": "Molestiae molestiae recusandae animi harum dolores quia. Et et voluptatum ea sunt dolor. Perferendis atque veniam totam quidem occaecati officiis expedita. Eum delectus a impedit.",
"progress": "20.74",
"status": 0,
"target_date": "2007-08-21",
"created_at": "2022-03-28T16:14:35.000000Z",
"updated_at": "2022-03-28T16:14:35.000000Z"
},
{
"id": 23,
"user_id": 25,
"title": "Quis tempora enim quo id.",
"description": "Nobis sed iste quia et sit facere sit. Et atque recusandae perspiciatis quod aspernatur minus. Minima illo doloremque et animi. Est ea autem soluta. Ut iusto dolor autem non. Veniam fugiat est dolorem fugiat laudantium explicabo.",
"progress": "19.48",
"status": 0,
"target_date": "2010-12-22",
"created_at": "2022-03-28T16:14:35.000000Z",
"updated_at": "2022-03-28T16:14:35.000000Z"
}
],
[
{
"id": 61,
"user_id": 25,
"title": "Officiis ut in vitae minus.",
"description": "Magni quis sint eaque quod. Modi soluta asperiores ipsum nobis praesentium. Suscipit asperiores vero doloribus iure. Hic possimus vel asperiores temporibus quo ea.",
"progress": "63.42",
"status": 1,
"target_date": "2016-01-27",
"created_at": "2022-03-28T16:14:35.000000Z",
"updated_at": "2022-03-28T16:14:35.000000Z"
}
]
]
但是,例如,当我请求 ID 为 26 的用户时,会返回此值。
{
"2": [
{
"id": 29,
"user_id": 26,
"title": "Labore placeat voluptatum et voluptatem.",
"description": "Labore incidunt consequatur ab cupiditate ducimus distinctio aut. Laboriosam neque et odio nostrum ab voluptas et. In vero mollitia suscipit porro harum magni consequatur. Omnis nisi natus harum odio nostrum.",
"progress": "6.38",
"status": 2,
"target_date": "1986-01-25",
"created_at": "2022-03-28T16:14:35.000000Z",
"updated_at": "2022-03-28T16:14:35.000000Z"
}
],
"1": [
{
"id": 44,
"user_id": 26,
"title": "Dicta est suscipit in sapiente sed architecto.",
"description": "Aperiam ab dolorum ut id voluptate. Autem earum eum recusandae quaerat repellat officiis. Quo provident corporis doloribus qui molestias. Sunt non modi facilis numquam optio officia ea.",
"progress": "79.49",
"status": 1,
"target_date": "2018-04-28",
"created_at": "2022-03-28T16:14:35.000000Z",
"updated_at": "2022-03-28T16:14:35.000000Z"
}
]
}
如有任何帮助,我们将不胜感激。
使用 laravels values() 函数修复了问题
return response()->json($goals->values(), 200);
目前我正在开发一个员工管理系统,该系统具有存储用户目标的功能。
每次返回数据时,根据哪个用户,数据JSON格式不一致,因此无法在前端正常显示。
控制器代码是
public function index(Request $request){
$user = $request->user();
if($request->query('user')){
$user = User::find($request->query('user'));
$goals = $user->goals->groupBy('status');
return response()->json($goals, 200);
}
$goals = $user->goals->groupBy('status');
if(!$goals){
return response()->json([
"Message" => "No goals found."
], 404);
}
return response()->json($goals, 200);
}
在用户模型上定义了目标关系:
public function goals(){
return $this->hasMany(Goal::class, 'user_id');
}
当我请求目标 ID 为 25s 的用户时,我得到了这个响应(我想要的格式)。
[
[
{
"id": 22,
"user_id": 25,
"title": "Quia quibusdam nisi aperiam harum.",
"description": "Molestiae molestiae recusandae animi harum dolores quia. Et et voluptatum ea sunt dolor. Perferendis atque veniam totam quidem occaecati officiis expedita. Eum delectus a impedit.",
"progress": "20.74",
"status": 0,
"target_date": "2007-08-21",
"created_at": "2022-03-28T16:14:35.000000Z",
"updated_at": "2022-03-28T16:14:35.000000Z"
},
{
"id": 23,
"user_id": 25,
"title": "Quis tempora enim quo id.",
"description": "Nobis sed iste quia et sit facere sit. Et atque recusandae perspiciatis quod aspernatur minus. Minima illo doloremque et animi. Est ea autem soluta. Ut iusto dolor autem non. Veniam fugiat est dolorem fugiat laudantium explicabo.",
"progress": "19.48",
"status": 0,
"target_date": "2010-12-22",
"created_at": "2022-03-28T16:14:35.000000Z",
"updated_at": "2022-03-28T16:14:35.000000Z"
}
],
[
{
"id": 61,
"user_id": 25,
"title": "Officiis ut in vitae minus.",
"description": "Magni quis sint eaque quod. Modi soluta asperiores ipsum nobis praesentium. Suscipit asperiores vero doloribus iure. Hic possimus vel asperiores temporibus quo ea.",
"progress": "63.42",
"status": 1,
"target_date": "2016-01-27",
"created_at": "2022-03-28T16:14:35.000000Z",
"updated_at": "2022-03-28T16:14:35.000000Z"
}
]
]
但是,例如,当我请求 ID 为 26 的用户时,会返回此值。
{
"2": [
{
"id": 29,
"user_id": 26,
"title": "Labore placeat voluptatum et voluptatem.",
"description": "Labore incidunt consequatur ab cupiditate ducimus distinctio aut. Laboriosam neque et odio nostrum ab voluptas et. In vero mollitia suscipit porro harum magni consequatur. Omnis nisi natus harum odio nostrum.",
"progress": "6.38",
"status": 2,
"target_date": "1986-01-25",
"created_at": "2022-03-28T16:14:35.000000Z",
"updated_at": "2022-03-28T16:14:35.000000Z"
}
],
"1": [
{
"id": 44,
"user_id": 26,
"title": "Dicta est suscipit in sapiente sed architecto.",
"description": "Aperiam ab dolorum ut id voluptate. Autem earum eum recusandae quaerat repellat officiis. Quo provident corporis doloribus qui molestias. Sunt non modi facilis numquam optio officia ea.",
"progress": "79.49",
"status": 1,
"target_date": "2018-04-28",
"created_at": "2022-03-28T16:14:35.000000Z",
"updated_at": "2022-03-28T16:14:35.000000Z"
}
]
}
如有任何帮助,我们将不胜感激。
使用 laravels values() 函数修复了问题
return response()->json($goals->values(), 200);