【发布时间】:2021-09-27 17:35:25
【问题描述】:
我想将$users 中的数据作为数组返回,并将$this->guard()->user() 中的数据作为不在数组中的数据返回。我在下面尝试过。但它会产生错误 - Argument 2 passed to Symfony\\Component\\HttpFoundation\\JsonResponse::__construct() must be of the type int, object given, called in C:\\wamp64\\www\\Laravel\\projects\\Content-Que-Laravel-API\\vendor\\laravel\\framework\\src\\Illuminate\\Http\\JsonResponse.php on line 32。我正在使用 Laravel-8 和 pgsql -13。
如何解决这个问题?任何建议..
$users = DB::table('users')
->join('location', 'users.id', '=', 'location.id')
->join('user_technical_details', 'users.id', '=', 'user_technical_details.id')
->get();
return response()->json(['data' => $users],$this->guard()->user());
预期输出:
{
"id": 2,
"name": "mrt",
"email": "ba@f.com",
"phone": 1112223333,
"email_verified_at": null,
"OTP": null,
"ph_OTP": 6033,
"email_OTP": 1090,
"user_access_token": null,
"created_at": "2021-07-20T06:49:29.000000Z",
"updated_at": "2021-07-20T06:49:29.000000Z"
}
【问题讨论】:
-
你说的“不在数组中”是什么意思?您能否向我们展示一个预期响应的示例?展示比模糊的解释更有效。无论哪种方式,该函数的第二个参数都必须是一个整数(它似乎是您可以设置响应的 HTTP 状态的地方)。您要以 json 形式返回的所有数据都必须在第一个参数中。
-
@MagnusEriksson 查看更新后的问题。我添加了示例输出
-
输出的哪些部分来自
$users,哪些部分来自$this->guard()->user()?data在哪里?请向我们展示 full 响应(格式)的示例。这似乎只是其中的一部分。
标签: php arrays json laravel laravel-8