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

closer01's avatar

How to show throttle message like session flash

Hi, I use laravel 5.4, and i want to limit request of the route like below

Route::group(['middleware' => ['throttle:2']], function () {

    Route::post('/xxx', 'TestController@getTest');

});

It works well but when received "Too Many Attempts." it shows in a blank page. Is there way to display this something like below?

session()->flash('message', ' Too Many Attempts.!');
        return redirect('/home');

Thanks

0 likes
1 reply
ignaciodev's avatar

I know this is a really old question, but this is how I solved this today on Laravel 10:

class ThrottleRequestsAndFlash extends ThrottleRequests
{
    /**
     * Handle an incoming request.
     */
    public function handle($request, Closure $next, $maxAttempts = 60, $decayMinutes = 1, $prefix = '')
    {
        try {
            return parent::handle($request, $next, $maxAttempts, $decayMinutes, $prefix);
        }
        catch (ThrottleRequestsException $exception) {
            $timeSuffix = (string) $decayMinutes === (string) 1 ? "minute" : "minutes";
            
            return back()
                ->withHeaders($exception->getHeaders())
                ->with('errorMessage', "Too many requests, please try again in {$decayMinutes} {$timeSuffix}");
        }
    }
}
1 like

Please or to participate in this conversation.