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

JennySwift's avatar

How to use Laravel Throttle

Hi, I am trying to use Laravel Throttle but am having trouble getting it work and wondering if someone could please help me?

I've followed the installation and configuration instructions. But when I try the following example code from the docs (replacing 'foo' with my own path, and using smaller numbers for the throttle parameters), it doesn't seem to work:

use Illuminate\Support\Facades\Route;

Route::get('foo', array('before' => 'throttle:50,30', function () {
    return 'Why herro there!';
}, ));

I have also tried this other example from the docs, and each time I refresh the page, count($throttler) is still 1:

use GrahamCampbell\Throttle\Facades\Throttle;
use Illuminate\Support\Facades\Request;

// let's quickly get the current request object
$request = Request::getFacadeRoot();

// now let's get a throttler object for that request
// we'll use the same config as in the previous example
// note that only the first parameter is "required"
$throttler = Throttle::get($request, 50, 30);

// let's check if we've gone over the limit
var_dump($thottler->check());

// we implement Countable
var_dump(count($thottler));

I'm assuming $thottler is a typo in the example and should be $throttler.

I figure count($throttler) should increment each time I load the page? But I'm not sure how to get it to do that.

0 likes
10 replies
alexwolff's avatar

What ind of cache driver do you use? I'm using redis and it works fine, I set up my filter directly in the controller.

public function __construct (Guard $auth)
    {

        $this->auth = $auth;


        $this->middleware('admin.guest', ['except' => 'getLogout']);

        $this->beforeFilter('throttle:5,1', ['only' =>
                                                 ['postLogin']]);
    }

thats what my auth controller looks like. and it works like expected. after the 5 times hitting the route with wrong credetials i get an "Rate limit exceed." error instead of the "These credentials do not match our records." error.

1 like
JennySwift's avatar

Hi Alex, thanks for the reply! I am using the memcached cache driver. I just did this in my .env file:

CACHE_DRIVER=memcached

So I tried adding this to AuthController.php, and still nothing would stop me from trying to login many times with the wrong password:

$this->beforeFilter('throttle:2,1', ['only' => ['postLogin']]);

I suppose it could be something really basic I am not doing. I'm quite new to Laravel. My PHP is ok but I'm not so familiar with object-oriented PHP yet so I find it hard to read and understand Laravel's code.

clin407's avatar

Hi @JennySwift, did you ever figure this out? Would love to hear your reply since I'm attempting the same thing.

JennySwift's avatar

Hi @clin407, sorry for the late reply. I only just noticed your comment.

I didn't, but @foxted helped me get it working :).

routes.php:

Route::group(['prefix' => 'auth', 'namespace' => 'Auth'], function(){

    Route::group(['middleware' => 'guest'], function(){
        // Login
        Route::get('login', ['as' => 'auth.login', 'uses' => 'AuthController@getLogin']);
        Route::post('login', ['as' => 'auth.login.store', 'before' => 'throttle:2,60', 'uses' => 'AuthController@postLogin']);

        // Register
        Route::get('register', ['as' => 'auth.register', 'uses' => 'AuthController@getRegister']);
        Route::post('register', ['as' => 'auth.register.store', 'uses' => 'AuthController@postRegister']);
    });

    Route::group(['middleware' => 'auth'], function(){
        // Logout
        Route::get('logout', ['as' => 'auth.logout', 'uses' => 'AuthController@getLogout']);
    });

});

Route::controllers([
    'password' => 'Auth\PasswordController',
]);

app/Exceptions/Handler.php:

use Exception, Redirect;
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;

public function render($request, Exception $e)
    {
        if ($e instanceof TooManyRequestsHttpException)
           {
               return Redirect::back()
                    ->withInput($request->only('email', 'remember'))
                    ->withErrors([
                        'email' => 'Too many failed login attempts!',
                ]);
           }

           return parent::render($request, $e);
    }
2 likes
mkaya's avatar

@JennySwift did you find any solution because i have got same problem if you have could you share with me thanks

gbrits's avatar

While using the instructions out of the box described on https://github.com/GrahamCampbell/Laravel-Throttle I found the following route to work for me straight away, whereas I ran into non-response using the route setup described on the git readme.

Route::group(['middleware' => 'throttle:3,1'], function() {
        Route::get('/', function() {
                return 'All good';
        });
});

Please or to participate in this conversation.