Search code examples
phplaraveleloquentlaravel-8laravel-7

laravel update not working inside login function


// 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);
        }

above is the method i have tried on laravel 8. the question is i want to increase the attempts column value by one if email is correct and password is wrong.


Solution

  • 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;
    }
    

    i have included DB::raw('attempts-1')