Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

azbx's avatar
Level 17

Rate limiter not working properly.

I have setup a custom rate limiter in laravel 11.

it looks like it should work but it's not.

what should I do?

1 like
18 replies
azbx's avatar
Level 17

@Tray2 heres that code.

$executed = RateLimiter::attempt(
            $key,
            $limit,
            function () use ($request) {
            if ($request->message != Auth::user()->latestStatus) {
                // Logging
                $status = new Status;
                $status->creator_id = Auth::id();
                $status->message = $request->message;
                $status->save();

              
                $user = User::find(Auth::id());
                $user->status = $request->message;
                $user->save();
                return response()->json(
                    [
                        'message' => 'Your status has been changed.',
                        'type' => 'success'
                    ]
                );
            }

        }, 60);
Snapey's avatar

it looks like it should work but it's not.

please explain what happens

1 like
azbx's avatar
Level 17

@Snapey I apologise for the confusion, basically when I make a request, the key is not incremented. So when I make the 5 requests needed, it doesn't block the request. I've tried incrementing before the status function is ran but it doesn't work as well.

1 like
benridev's avatar

I've tested with your source code, but there was nothing wrong with the RateLimiter.
"when I make the 5 requests needed, it doesn't block the request" => Am I right to understand that you've made 5 requests, but when you tried the 6th request, it still was not blocked.( In my place, the 6th request was blocked)

1 like
azbx's avatar
Level 17

@benridev I dont know what im doing wrong at this point. Do I have to define the rate limit in the app service provider?

i also have a login rate limiter and that doesn't work as well. so its not just this code.

1 like
Snapey's avatar

bumpety bumppety bump

1 like

Please or to participate in this conversation.