2016-05-06 2 views
1

Я объявил в моей миграции пользователя о том, что пользователь должен быть уникальным:Laravel дубликат электронной

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

Но когда я делаю пользователь в первый раз в почтальонов я получаю следующее сообщение об ошибке:

{ 
    "message": "SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '[email protected]' 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, [email protected], 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; 
    } 

Я использую динго и JWT-Auth.

+0

Не забудьте добавить функцию 'validateUser()'. Кроме того, просто добавить уникальное не достаточно, вы должны добавить уникальную проверку в запрос или 'validator :: make()' –

ответ

1

Посмотрите:

`if(!$validator != null)` 

Вам не нужно двойное отрицание. Вы можете просто дать:

`if(is_null($validator))` 

То, как вы получите двойное save действие.

+0

Thankyou, который отлично работает. – Jamie

Смежные вопросы