简体   繁体   English

如何在 Auth::attempt 方法中传递 OR?

[英]How can I pass OR in Auth::attempt method?

I have Laravel project and I want to allow users to log in using a username or email but I haven't found any way to pass Or Condition in Auth::attempt() method.我有 Laravel 项目,我想允许用户使用用户名或电子邮件登录,但我还没有找到任何方法在 Auth::attempt() 方法中传递或条件。

I used below code for logged via email only,我使用以下代码仅通过电子邮件登录,

Auth::attempt(['email' => request('email'), 'password' => request('password')])

You can check with condition of email validation check您可以检查电子邮件验证检查的条件

Example:例子:

$field = filter_var($request->email, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';

if (Auth::attempt([$field => $request->email, 'password' => $request->password], true)) {
    // ...
}

You can find more about FILTER_VALIDATE_EMAIL here您可以在此处找到有关 FILTER_VALIDATE_EMAIL 的更多信息

If the first attempt fails, do another attempt against username:如果第一次尝试失败,请针对用户名再次尝试:

if (Auth::attempt(['email' => request('email'), 'password' => request('password')])
 || Auth::attempt(['username' => request('email'), 'password' => request('password')]))
{
   // Login success..
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM