Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Usage Limiter Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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.

Implementation Patterns

  • Plan-based rate limiting: Define named limits (e.g., 'projects', 'api_calls') with associated plans ('free', 'pro', 'enterprise'). Use setLimit() to bind limits to users/accounts dynamically.
  • Automatic usage accounting: Hook into model lifecycle events (created, deleted) to call useLimit()/unuseLimit() for resources like storage uploads, team memberships, or API endpoints.
  • Scheduling: Register the 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.
  • Middleware enforcement: Wrap critical endpoints in custom middleware that checks hasEnoughLimit() before processing—return 429 if exhausted.
  • Blade-based UI controls: Use the @limit directive to conditionally render UI elements (e.g., disable “Create Project” buttons) based on remaining quota.

Gotchas and Tips

  • Exceptions on overuse: useLimit()/unuseLimit() throw exceptions on exceeding limits or negative usage—wrap calls in try/catch or use hasEnoughLimit() first to avoid unhandled errors.
  • Plan matching is critical: If you forget the plan parameter when calling setLimit() or useLimit(), it may default to null and not match your intended plan-specific limit—be explicit.
  • Reset frequency requires scheduling: Only set 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.
  • Caching: By default, limits are cached for 24 hours. Use limit:cache-reset or flushCache() after bulk limit changes (e.g., admin plan upgrades) to force refresh.
  • Extension point: If you need custom behavior (e.g., soft-deleting limits), extend the Limit model and override in config/limit.php, but remember to clear config cache.
  • DB column types: Ensure your used_amount column supports decimals (DECIMAL(10,2) or similar) if using non-integer amounts (e.g., storage in GB).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport