从jsonResponse Laravel 8中获取数据值



我有两个控制器第一个有一个寄存器方法:

public function register(Request $request)
{
$generalTrait = new GeneralTrait;
$user = new User;
$user->email = $request->email;
$user->password = bcrypt($request->password);
$user->type = $request->type;
$user->save();
return $generalTrait->returnData('user',$user);
}

第二种也有注册方法:

public function register(Request $request)
{
$generalTrait = new GeneralTrait;
$user = (new UserAuthController)->register($request);
$admin = Admin::create([
'admin_name' => $request->admin_name,
//'user_id' => $response->user->user_id,
'user_id' => $user_id
]);
//Admin created, return success response
return $generalTrait->returnSuccessMessage('Admin created successfully');
}

当我试图从(JsonResponse($user获取数据时,我发现了这个错误:

ErrorException: Undefined property: IlluminateHttpJsonResponse::$user

returnDate方法在GeneralTrait中返回:

public function returnData($key, $value, $msg = ""){
return response()->json([
'status' => true,
'errNum' => "5000",
'msg' => $msg,
$key => $value
]);
}

当我试图从$response获取状态时,我发现了相同的错误我该怎么修?

我通过将returnData替换为:来修复它

public function returnData($key, $value, $msg = ""){
return [
'status' => true,
'errNum' => "5000",
'msg' => $msg,
$key => $value
];}

所以为了从用户那里获得user_id,我说:

'user_id' => ($response["user"])->user_id

我希望我知道我的错误是什么,以及我如何以其他方式纠正

相关内容

  • 没有找到相关文章

最新更新