This is an example of how you can setup rate limiting in Laravel
phplaravel
Rate Limiting
// In a controller method
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
public function handle(Request $request)
{
$key = Str::lower($request->ip()) . '|' . $request->path();
if (RateLimiter::tooManyAttempts($key, 5)) {
return response()->json([
'message' => 'Too many requests. Try again later.',
], 429);
}
RateLimiter::hit($key, 60); // 5 attempts per 60 seconds
// Proceed with logic
return response()->json([
'message' => 'Request successful!',
]);
}