laravel 更新在登录功能中不起作用

时间:2021-05-21 23:29:48

标签: php laravel eloquent laravel-8 laravel-7

// Request of email and password
        $credentials = request(['email', 'password']);

// Checking credentials and increasing attempt value by 1
        if (!Auth::attempt($credentials)) {
            User::where('email',   $request->email)->update(['attempts' => 'attempts+1']);
            return response()->json([
                'message' => 'Unauthorized access',
            ], 401);
        }

以上是我在 laravel 8 上尝试过的方法。问题是如果电子邮件正确且密码错误,我想将尝试列值增加一。

1 个答案:

答案 0 :(得分:0)

if (!Auth::attempt($credentials)) {
        $this->reduceAttempt($request->email);
        return response()->json([
            'message' => 'Unauthorized access',
        ], 401);
    }



public function reduceAttempt($email)
{

    User::where('email',  $email)->update(['attempts' => DB::raw('attempts-1')]);
    return  $email;
}

我已经包含了 DB::raw('attempts-1')