samedi 19 septembre 2020

The email has already been taken. Even i try to add new email address

I am facing very uncertain issues with Laravel 5.8. While creating user using User::create();

If I set the validation for a unique email so it will return me error i.e {"message":"The given data was invalid.","errors":{"email":["The email has already been taken."]}} else it's insetting 4 records in the database for the same email address.

Validation code is :

$request->validate([
            'name'         => 'required',
            'branch'       => 'required',
            'role'         => 'required',
            'status'       => 'required',
            'super_module' => 'required',
            'password'     => 'required|min:5',
            //'email'        => 'required|email'
            'email'        => 'required|email|unique:users'
        ], [
            'branch.required'       => 'Please select branch.',
            'role.required'         => 'Please select role.',
            'status.required'       => 'Status is required',
            'super_module.required' => 'Please select super module.',
            'name.required'         => 'Name is required',
            'password.required'     => 'Password is required'
        ]);

Here is my code

try {
      if(!empty($request->institute)){
        $userData['institute_id'] = $request->institute;
        if(!empty($request->user_type)){
          $userData['institute_user_type_id'] = $request->user_type;
        }
      }
      $userData['type']          = 'user';
      $userData['name']          = $request->name;
      $userData['email']         = $request->email;
      $userData['password']      = bcrypt($request->password);
      $userData['password_view'] = $request->password;
      $userData['branch_id']     = $request->branch;
      $userData['status']        = $request->status;
      $userData['created_by']    = Auth::user()->id;
      // echo "<pre>";print_r($userData);echo "</pre>";die;
      $user                      = User::create($userData);
      if(@$user->id >0){
          $message['type']    = 'POST';
          $message['success'] = 'User create successfully';
      }else{
        $routeName                = Route::getCurrentRoute();
        $message['type']          = 'POST';
        $message['user_id']       = Auth::user()->id;
        $message['opretion']      = 'Login User Create';
        $message['error_message'] = 'User create failed';
        $message['url']           = $routeName->action['controller'];
        Logs::create($message);
      }
      return Response()->json($message);
} catch (\Exception $ex) {
    $routeName                = Route::getCurrentRoute();
    $message['type']          = 'POST';
    $message['user_id']       = Auth::user()->id;
    $message['opretion']      = 'Login User Create';
    $message['error_message'] = $ex->getMessage();
    $message['url']           = $routeName->action['controller'];
    Logs::create($message);
    return Response()->json($message);
}


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire