Rate Limiting in laravel to x times per day using RateLimiter

826 Views Asked by At

I am trying to restrict usage of a certain endpoint to 10 times per day per user, and I can see how to do it for a certain number of times per minute. Please note that this is within a controller, not as middleware.

    $executed = RateLimiter::attempt(
        'user-send-test-' . Auth::user()->id,
        $perDay = 10,
        function () {
            // some process
        }
    );
1

There are 1 best solutions below

0
N69S On

To use the rate limiter per day, use the fourth parameter of the method attemp() which is the decay (how long an attempt need to be counted for) but I never tested it.

$executed = RateLimiter::attempt(
    'user-send-test-' . Auth::user()->id,
    $perDay = 10,
    function () {
        // some process
    },
    60*60*24 // or 86400
    );

If you still encounter issues with this example, I suggest you copy the RateLimiter class as it is very small and adapt its code.