Laravel重复电子邮件

时间:2016-05-06 13:06:51

标签: php laravel

我在用户迁移中声明用户必须是唯一的:

$table->string('email', 30)->unique();

但是当我第一次在邮差中创建用户时,我收到以下错误:

{
  "message": "SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'james@gest.com' for key 'user_email_unique' (SQL: insert into `user` (`firstname`, `lastname`, `displayname`, `email`, `phonenumber`, `birthdate`, `profilepicture`, `suspended`, `role_id`, `company_id`, `updated_at`, `created_at`) values (james, egen, james egen, james@gest.com, 0628383493, , , 1, 1, 1, 2016-05-06 13:03:31, 2016-05-06 13:03:31))",
  "code": "23000",
  "status_code": 500,

当我查看我的数据库时,用户是这样做的,为什么它会给我这个错误?

控制器:

  public function store(Request $request)
    {
        $result = $this->userRepo->store($request);

        if (!$this->userRepo->store($request) != null) {
            return response()->json(['message' => 'Gebruiker is succesvol aangemaakt'], 200);
        }
        return response()->json(['message' => $result], 406);
    }

userRepo:

  public function store(Request $request)
    {
        $validator = $this->validateUser($request);

        if(!$validator != null)
        {
            $user = new User([
                'firstname'             => $request->firstname,
                'lastname'              => $request->lastname,
                'displayname'           => $request->displayname,
                'email'                 => $request->email,
                'phonenumber'           => $request->phonenumber,
                'birthdate'             => $request->birthdate,
                'profilepicture'        => $request->profilepicture,
                'suspended'             => $request->suspended,
                'role_id'               => $request->role_id,
                'company_id'            => $request->company_id
            ]);

            $user->save();
            return null;
        }
        return $validator;
    }

我正在使用dingo和JWT-Auth。

1 个答案:

答案 0 :(得分:1)

查找

`if(!$validator != null)`

你不需要双重否定。你可以给:

`if(is_null($validator))`

这就是你获得双save行动的方式。