I want to rate limit per day instead of per minute. How do I do it?
if (RateLimiter::remaining('send-message:'.$user->id, $perMinute = 5)) {
RateLimiter::hit('send-message:'.$user->id);
// Send message...
}
CodePudding user response:
Just add another limiter in app\Providers\RouteServiceProvider.php
inside configureRateLimiting
function
like so;
protected function configureRateLimiting() {
//Default Limiter
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
});
//Your Limiter - limit 1 request per day
RateLimiter::for('day', function (Request $request) {
return Limit::perDay(10)->by(optional($request->user())->id ?: $request->ip());
});
}
then your routes, you can use it for throttle
e.g.
Route::group(['middleware' => 'throttle:day'], function () {
Route::get('/limit-test', function( \Illuminate\Http\Request $request) {
return $request;
})->name('rate.test');
});
If you check the response header, you should be able to see
X-RateLimit-Limit: 10
X-RateLimit-Remaining: 9
CodePudding user response:
you can use this code
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
});