我的Laravel应用程序有一个奇怪的问题,一旦验证规则启动,这个代码会被调用两次。我将验证逻辑抽象到一个单独的类中,但无论我如何使用API(尝试使用Postman,和使用jQuery)它似乎仍然运行两次,输出看起来像这样:
viewDidDisappear
我只期待一个JSON响应。我正在撕扯我的头发,尝试了两个不同的连接,似乎无法解决为什么自定义请求被调用两次。这是一个新的Laravel应用程序,因此没有太多代码可以与之冲突。任何想法都赞赏。
called{"email":["The email has already been taken."],"country":["The country must be a number."]}called{"email":["The email has already been taken."],"country":["The country must be a number."]}
然后验证自定义请求..
//Create User Request extends standard request. Handles Validation
public function __construct(CreateUserRequest $request){
$this->request = $request;
}
public function register()
{
try{
$array = DB::transaction(function(){
$email = $this->request->input('email');
$password = $this->request->input('password');
$companyName = $this->request->input('companyName');
$userName = $this->request->input('name');
$country = $this->request->input('country');
$company = Company::create([
'name' => $companyName,
'active'=>true,
'country_id'=>$country
]);
$user = User::create([
'company_id' => $company->id,
'name'=>'admin',
'email' => $email,
'password' => $password,
'active' =>true
]);
if( !$company || !$user )
{
throw new \Exception('User not created for account');
}
return compact('company', 'user');
});
$token = JWTAuth::fromUser($array['user']);
return Response::json(compact('token'));
}
catch( Exception $e )
{
return Response::json(['error' => $e->getMessage() ], HttpResponse::HTTP_CONFLICT );
}
}
答案 0 :(得分:2)
有趣。
尝试从CreateUserRequest $request
中删除__construct()
参数,并将其添加到您的register()
方法中,如下所示:register(CreateUserRequest $request)
。并通过拨打$request
而不是$this->request
来使用您的请求。