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

Posted

技术标签:

【中文标题】laravel 更新在登录功能中不起作用【英文标题】:laravel update not working inside login function 【发布时间】:2021-08-11 03:35:59 【问题描述】:
// 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】:
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')

【讨论】:

仅代码的答案通常不受欢迎,您能解释一下您的更改以及为什么它可以解决问题吗? 完成@prieber。

以上是关于laravel 更新在登录功能中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

updateOrCreate在Laravel Eloquent中不起作用

Laravel 审核更新事件不起作用

Laravel 路由在 API 中不起作用,返回 404 页

LARAVEL 5.8:窗口滚动,滚动在刀片模板中不起作用

ajax 类型的帖子在 laravel 5.4 中不起作用

路由到子文件夹中的控制器在 Laravel 4 中不起作用