我在注册新用户方面遇到了挑战。我从Android应用程序(API)访问BackEnd。
我正在使用此Laravel API Boilerplate
这是我的验证规则;
'signup_fields_rules' => [
'sname' => 'required',
'fname' => 'required',
'gender' => 'required',
'email' => 'required|email|unique:users,email',
'password' => 'required|min:8',
],
和我的新用户注册码
public function signup(Request $request)
{
$signupFields = Config::get('boilerplate.signup_fields');
$hasToReleaseToken = Config::get('boilerplate.signup_token_release');
$userData = $request->only($signupFields);
$validator = Validator::make($userData, Config::get('boilerplate.signup_fields_rules'));
if($validator->fails()) {
Log::info($validator->errors()->all());
throw new StoreResourceFailedException('Could not create account.',$validator->errors()->all());
}
User::unguard();
$dept = DB::table('depts')->select('dept_id')
->where('dept_name', $request->input('dept'))->first();
$userData['user_id'] = str_random(60);
$userData['dept_id'] = $dept->dept_id;
$user = User::create($userData);
User::reguard();
if(!$user->id) {
return $this->response->error('could_not_create_user', 500);
}
if($hasToReleaseToken) {
return $this->login($request);
}
return $this->response->created();
}
问题:
当新用户尝试注册时,会显示Email taken already
。我发现在显示错误之前,新的用户数据将存储在数据库中。注册函数是否被调用两次。
我需要帮助。