Laravel 输入验证存在用户名或电子邮件

Laravel Input Validaiton Exists with username or email

如果我从可以使用用户名或电子邮件登录的地方输入,如何进行正确验证,我想如果用户将在字段中输入用户名,它会检查该用户名是否不存在,然后失败,如果他将选择使用电子邮件登录,然后它将检查电子邮件是否不存在然后失败。 (登录表单只有 2 个输入,登录名和密码,登录时您可以输入用户名或电子邮件)

我的规则=

public function rules()
{
    return [
        'user'                => 'required|min:3|exists:users,username,email]',
        'password'            => 'required',
    ];
}

您可以使用自定义登录名:

public function login(Request $request)
{
    $request->validate([
        'user'                => 'required|min:3]',
        'password'            => 'required',
    ]);
}

$userName_or_Email = $request->user;
$password = $request->password;
$user = User::where('username',$userName_or_Email)->first();

if(empty($user)){
  $user = User::where('email',$userName_or_Email)->first();
}

if(empty($user)){
  return new Response()->with('error','Username or email not available in database');
}

if(Hash::check($password, $user->password)){
   // Login successful code
}else{
  return new Response()->with('error','Username or email not available in database');
}