nabilhassen/laravel-usage-limiter
Track and enforce per-model usage limits in Laravel. Define plan-based limits with reset intervals, consume/unconsume on resource changes, check remaining allowance, generate usage reports, and auto-reset via scheduled Artisan command.
Start by installing the package and publishing its config and migrations:
composer require nabilhassen/laravel-usage-limiter
php artisan vendor:publish --provider="NabilHassen\LaravelUsageLimiter\ServiceProvider"
php artisan migrate
Then add the HasLimits trait to your model (e.g., User). The first real-world use case is defining a basic plan-based limit—like 5 projects per month on the "standard" plan. After that, associate the limit with a user and begin tracking usage (e.g., useLimit() when a project is created). The hasEnoughLimit() and remainingLimit() methods provide immediate feedback for conditional logic in controllers or middleware.
'projects', 'api_calls') with associated plans ('free', 'pro', 'enterprise'). Use setLimit() to bind limits to users/accounts dynamically.created, deleted) to call useLimit()/unuseLimit() for resources like storage uploads, team memberships, or API endpoints.limit:reset Artisan command in app/Console/Kernel.php with frequency matching your limit reset rules (everyMinute for fine-grained limits, everyDay for daily caps). Supports Laravel 10+ everySecond.hasEnoughLimit() before processing—return 429 if exhausted.@limit directive to conditionally render UI elements (e.g., disable “Create Project” buttons) based on remaining quota.useLimit()/unuseLimit() throw exceptions on exceeding limits or negative usage—wrap calls in try/catch or use hasEnoughLimit() first to avoid unhandled errors.plan parameter when calling setLimit() or useLimit(), it may default to null and not match your intended plan-specific limit—be explicit.reset_frequency if you run the limit:reset command regularly; otherwise, usage accumulates indefinitely. Laravel 10+ allows everySecond, but be cautious about performance in high-traffic apps.limit:cache-reset or flushCache() after bulk limit changes (e.g., admin plan upgrades) to force refresh.Limit model and override in config/limit.php, but remember to clear config cache.used_amount column supports decimals (DECIMAL(10,2) or similar) if using non-integer amounts (e.g., storage in GB).How can I help you explore Laravel packages today?