当我使用数据库中不存在的电子邮件创建新帐户时,显示重复条目错误代码

时间:2019-06-11 11:22:48

标签: laravel-5

我用Facebook创建了一个新帐户,为此我使用了新电子邮件,但显示了重复输入错误,但是我使用了数据库中不存在的新电子邮件,但他一次又一次抛出相同的错误。请帮助这是什么问题。提前致谢。

这是用Facebook登录的代码:

public function signInFacebook(SignInFacebookUser $request)
{
    if($profile_picture = $request->hasFile('profile_picture')) {
        $profile_picture = time().'.'.$request->profile_picture->getClientOriginalExtension();
        $request->profile_picture->move(public_path('ProfileImages'), $profile_picture);
        $profile_picture = 'ProfileImages/'.$profile_picture;
    } else {
        $profile_picture = NULL;
    }

    $user = User::withTrashed()->where('facebook_id', $request->get('facebook_id'))->first();

    if ($user) {
        if ($user->trashed()) {
            $user->restore();
            $user->buckets()->restore();
            $user->posts()->restore();
            $user->comments()->restore();
        }

        if ($user->account_status == 1) {
            try {
                $user->update([
                    'firstname' => $request->input('firstname'),
                    'lastname' => $request->input('lastname'),
                    'email' => $request->input('email'),
                    'profile_picture' => $profile_picture,
                ]);
                $user->save();
            } catch (QueryException $e) {
                $errorCode = $e->errorInfo[1];          
                if($errorCode == 1062){
                    return response()->json(['message' => 'An Account with this email already exist. Please enter a different account email']);
                }
            }

            $token = JWTAuth::fromUser($user);

            $userDetail = $user->where('id', $user->id)->with(['posts', 'buckets', 'likes', 'followers', 'following'])->withCount('posts', 'buckets','likes', 'followers', 'following')->first();
            // $defaultBucket['buckets'] = $this->defaultBuckets($user);
            // $userDetails = array_merge($userDetail->toArray(), $defaultBucket);
            // $userDetails = $this->userProfile($user);

            return response()->json(['status' => 'old user', 'token' => $token, 'user' => $userDetail], 200);
        } else {
            return response()->json(['message' => 'You are not active on app, contact to support team'], 200);
        }
    } else {
        try {
            $user = User::create([
                'facebook_id' => $request->input('facebook_id'),
                'firstname' => $request->input('firstname'),
                'lastname' => $request->input('lastname'),
                'email' => $request->input('email'),
                'profile_picture' => $profile_picture,
            ]);
        } catch (QueryException $e) {
            $errorCode = $e->errorInfo[1];          
            if($errorCode == 1062){
                return response()->json(['message' => 'An Account with this email already exist. Please enter a different account email']);
            }
        }

        $textmsg = "Thanks for signing up to KindMill we hope you enjoy our app! If you did not setup an account please send us an email at ​support@kindmill.com ";

        $token = JWTAuth::fromUser($user);

        $userDetail = $user->where('id', $user->id)->with(['posts', 'buckets', 'likes', 'followers', 'following'])->withCount('posts', 'buckets','likes', 'followers', 'following')->first();
        // $defaultBucket['buckets'] = $this->defaultBuckets($user);
        // $userDetails = array_merge($userDetail->toArray(), $defaultBucket);
        // $userDetails = $this->userProfile($user);

        $defaultBucketNames = array(
            'Things I can do for charity',
            'Places to travel to',
            'Things around the home',
            'Things I want to learn',
            'Goals for this year',
            'Random things I need',
            'Things to do with friends'
        );
        foreach ($defaultBucketNames as $defaultBucketName) {
            $user->buckets()->create([
                'bucket_name' => $defaultBucketName,
                'bucket_type' => 'public',
                'bucket_image' => NULL,
            ]);
        }

        $userFollow = User::find(92);
        $user->follow($userFollow);
        $userfollow = User::find(100);
        $user->follow($userfollow);

        $this->getMessage->sendWelcomeMessage($userfollow, $user, $textmsg);

        return response()->json(['status' => 'new user', 'token' => $token, 'user' => $userDetail], 200);
    }

    return response()->json(['message' => 'failed to signin with facebook'], 200);   
}

此问题仅与创建新帐户有关。

0 个答案:

没有答案