🔴 Laravel
Intermediate
What is Rate Limiting in Laravel?
Answer
Rate limiting restricts how many times a user or IP can hit an endpoint within a time window. Apply the built-in throttle middleware: Route::middleware("throttle:60,1")->group(...) — 60 requests per minute. In Laravel 8+, define named rate limiters in RouteServiceProvider (or AppServiceProvider): RateLimiter::for("api", fn($request) => $request->user() ? Limit::perMinute(100)->by($request->user()->id) : Limit::perMinute(10)->by($request->ip())). Apply: Route::middleware("throttle:api"). Rate limiters can return custom responses on exceeded limits. The RateLimiter facade also provides attempt(), hit(), and tooManyAttempts() for manual rate limiting logic (e.g., login throttling).