Laravel Quota: Usage Budgets for Calendar Periods

Last updated on by

Laravel Quota: Usage Budgets for Calendar Periods image

Laravel Quota is a package for tracking and enforcing cumulative usage limits. For example, you could enforce limits like 50 PDF exports a month, 1,000 API queries a day, a pool of AI credits per billing cycle, and more.

It's aimed at a different problem than Laravel's RateLimiter: instead of throttling bursts of traffic in a sliding window, it counts consumption against a budget that resets on a calendar boundary, and it can persist that count in the database rather than only in the cache.

Here's what the package gives you:

  • Calendar-aligned periods — define perMinute(), perHour(), perDay(), perWeek(), perMonth(), perYear(), or period($start, $end) for a custom window
  • A HasQuotas trait that puts quotas on any Eloquent model
  • Route middleware (quota:exports,50,month) that only consumes on a successful response
  • Cache or database backends, switchable per call
  • Atomic consumption - lock consumption around the work it meters
  • Enforce budgets - throw an HTTP 429 when the budget is spent
  • And more.

The Fluent API

A quota is a named counter scoped to an owner, with a limit and a period. Once you've described one, you can inspect it or spend against it:

use ZaberDev\Quota\Facades\Quota;
 
$builder = Quota::for('api_queries', $user)
->limit(1000)
->perDay();
 
$builder->used();
$builder->remaining();
$builder->isExceeded();
$builder->hasCapacity(10);
 
$info = $builder->consume(5);

The consume() method returns an immutable QuotaInfo DTO. If you'd rather fail hard than branch, the enforce() method throws an HTTP 429 when the budget is gone:

Quota::for('api_queries', $user)->limit(1000)->perDay()->enforce();

For work where a double-spend actually matters, block() wraps the callback in a lock so two concurrent requests can't both slip through the capacity check:

Quota::for('pdf_generation', $user)
->limit(50)
->perMonth()
->block(function () use ($pdfService) {
$pdfService->generate();
}, amount: 1, lockSeconds: 30);

Quotas on Eloquent Models

Add the HasQuotas trait and the same builder hangs off the model:

use ZaberDev\Quota\HasQuotas;
 
class User extends Authenticatable
{
use HasQuotas;
}
$user->quota('pdf_exports')->limit(25)->perMonth()->consume();
 
$user->quota('pdf_exports')->limit(25)->perMonth()->remaining();

On the database backend the records are polymorphic, so you can query a model's quotas like any other relation:

$activeQuotas = $user->quotas()
->where('period_end', '>', now())
->get();

Route Middleware

The quota middleware takes a name, a limit, a period, and an optional driver:

Route::post('/exports/generate', [ExportController::class, 'store'])
->middleware('quota:exports,50,month');
 
Route::post('/api/v1/query', [ApiController::class, 'query'])
->middleware('quota:api_query,1000,day,database');

The ordering is worth noting: capacity is checked before the route runs, but the quota is only consumed when the response comes back 2xx or 3xx. A request that 500s or fails validation doesn't cost the user anything.

Storage Backends

The default driver is cache, backed by whatever store you've configured — Redis and Memcached both work. The database driver writes to a quotas table instead, which is what you want when the count is tied to billing and can't evaporate with a cache flush. You can pick per call:

Quota::for('api_ping', $ip)->using('cache')->limit(5000)->perDay()->consume();
 
Quota::for('monthly_exports', $user)->using('database')->limit(50)->perMonth()->consume();

Quota::extend() registers a custom backend from a service provider if you need somewhere else to put the counters, and expired database rows can be cleaned up on a schedule:

use ZaberDev\Quota\Models\Quota;
 
Schedule::command('model:prune', ['--model' => Quota::class])->daily();

Installation

The package requires PHP 8.2 and supports Laravel 11, 12, and 13:

composer require zaber-dev/laravel-quota
 
php artisan vendor:publish --provider="ZaberDev\Quota\QuotaServiceProvider"
php artisan migrate

The config/quotas.php configuration file sets the default driver (QUOTA_DRIVER), the cache store and key prefix, the database table name, and whether events are dispatched.

You can find installation instructions and full documentation on GitHub.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell

The latest

View all →
Find the Security Vulnerabilities in Your Laravel App with Sensagraph image

Find the Security Vulnerabilities in Your Laravel App with Sensagraph

Read article
Uvora: A macOS Menu Bar App for Finding Laravel Projects image

Uvora: A macOS Menu Bar App for Finding Laravel Projects

Read article
Passwordless Sign-In with Fortify Two-Factor Support in Laravel image

Passwordless Sign-In with Fortify Two-Factor Support in Laravel

Read article
Intercept: Middleware Guardrails for Laravel AI Agents image

Intercept: Middleware Guardrails for Laravel AI Agents

Read article
AI Review for Laravel Upgrades image

AI Review for Laravel Upgrades

Read article
HTTP Query Method Support in Laravel 13.19 image

HTTP Query Method Support in Laravel 13.19

Read article