在 Laravel 中哪里可以找到 Auth::attempt 方法

Man*_*san 3 php laravel

除了我的 Laravel 项目中用户的电子邮件和密码之外,我还想向身份验证查询添加额外的条件

我在官方 Laravel 文档中读到了这个。 https://laravel.com/docs/5.6/authentication

指定附加条件 如果您愿意,除了用户的电子邮件和密码之外,您还可以向身份验证查询添加附加条件。例如,我们可以验证用户是否被标记为“活跃”:

if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1])) {
    // The user is active, not suspended, and exists.
}
Run Code Online (Sandbox Code Playgroud)

我在哪里可以找到这种方法?

小智 6

您需要在 LoginController 中覆盖方法登录

public function login(\Illuminate\Http\Request $request) {
    $this->validateLogin($request);

    // If the class is using the ThrottlesLogins trait, we can automatically throttle
    // the login attempts for this application. We'll key this by the username and
    // the IP address of the client making these requests into this application.
    if ($this->hasTooManyLoginAttempts($request)) {
        $this->fireLockoutEvent($request);
        return $this->sendLockoutResponse($request);
    }

    // This section is the only change
    if ($this->guard()->validate($this->credentials($request))) {
        $user = $this->guard()->getLastAttempted();

        // Make sure the user is active
        if ($user->active && $this->attemptLogin($request)) {
            // Send the normal successful login response
            return $this->sendLoginResponse($request);
        } else {
            // Increment the failed login attempts and redirect back to the
            // login form with an error message.
            $this->incrementLoginAttempts($request);
            return redirect()
                ->back()
                ->withInput($request->only($this->username(), 'remember'))
                ->withErrors(['active' => 'You must be active to login.']);
        }
    }

    // If the login attempt was unsuccessful we will increment the number of attempts
    // to login and redirect the user back to the login form. Of course, when this
    // user surpasses their maximum number of attempts they will get locked out.
    $this->incrementLoginAttempts($request);

    return $this->sendFailedLoginResponse($request);
}
Run Code Online (Sandbox Code Playgroud)

这对我有用


lag*_*box 5

Auth::attempt你打电话的可能性最大Illuminate\Auth\SessionGuard@attempt

路径:

Auth::attempt -> Illuminate\Auth\AuthManager -> (guard) Illuminate\Auth\SessionGuard

Facade -> Illuminate\Auth\AuthManager@call -> @guard -> Illuminate\Auth\SessionGuard@attempt

Laravel 5.6 文档 - Facades - 类参考

为了能够调整为 传递的凭据,LoginController您只需要覆盖 1 个方法credentials,因为它是传递给 的数组所涉及的唯一方法attempt

protected function credentials(Request $request)
{
    return $request->only($this->username(), 'password')
        + ['active' => true];
}
Run Code Online (Sandbox Code Playgroud)