why can not find a user with that e-mail address?

前端 未结 4 751
耶瑟儿~
耶瑟儿~ 2021-01-27 09:46
public function create(Request $request) { 
    $this->validate($request, [ \'email\' => \'required|email\', ]);
    $user          


        
相关标签:
4条回答
  • 2021-01-27 10:16

    You can also try this.

    public function create(Request $request) { 
        $this->validate($request, [ 'email' => 'required|email', ]);
        $user = User::where('email', $request->email)->first();
        if (!$user) {
            return response()->json([
                'message' => 'We can not find a user with that e-mail address.' 
            ], 404);
        }
    }
    
    0 讨论(0)
  • 2021-01-27 10:25

    Try with isEmpty(), Like this :

    public function create(Request $request) { 
        
        $validatedData = $request->validate([
            'email' => 'required|email',
        ]);
    
        $user = User::where('email', $request->email)->first();
    
        if ($user->isEmpty())  {
            return response()->json([
                 'message' => 'We can not find a user with that e-mail address.' 
            ], 404);
         } else {
              return response()->json([
                 'message' => 'User is found'], 201 );
         }
    }
    
    0 讨论(0)
  • 2021-01-27 10:30

    Try doing this.

    This will return a success with user found if user is actually in DB with email or return 404 with NO user found.

    public function create(Request $request) { 
        $this->validate($request, [ 'email' => 'required|email', ]);
        $user = User::where('email', $request->email)->first();
        if ($user) {
            return response()->json([
                'message' => 'User Found'
             ], 200);
        } else {
            return response()->json([
                'message' => 'We can not find a user with that e-mail address.' 
             ], 404);
        }
    }
    
    0 讨论(0)
  • 2021-01-27 10:38

    simply use the exists method of validation

    public function create(Request $request) { 
    
           $validatedData = $request->validate([
                'email' => 'required|email|exists:user,email',
           ]);
    
    }
    
    0 讨论(0)
提交回复
热议问题