nabilhassen/laravel-usage-limiter
Track, limit, and restrict usage for users/accounts or any model in Laravel. Define per-plan limits with reset frequencies, consume/unconsume on create/delete, check remaining allowance, generate usage reports, and auto-reset via scheduled Artisan command.
HasLimits) and model relationships for seamless integration.User, Account, or custom models).useLimit()) and explicit triggers (e.g., post-create hooks), fitting both reactive and proactive architectures.everySecond() scheduling).User).@limit syntax for UI integration without custom logic.| Risk Area | Mitigation |
|---|---|
| Laravel version lock | Explicitly supports Laravel 12+, with active updates (e.g., v2.0.0 for Laravel 13). Risk: Low if using supported versions. |
| Customization limits | Extensible via config (e.g., limit.php for table/model names, cache stores). Risk: Medium for edge cases (e.g., multi-dimensional limits). |
| Concurrency issues | Uses database transactions for useLimit()/unuseLimit() to prevent race conditions. Risk: Low if following best practices (e.g., retries for deadlocks). |
| Performance at scale | Caching reduces DB load, but high-frequency limits (e.g., "every second") may require tuning. Risk: Low for typical SaaS use cases; monitor cache hit ratios. |
| Migration complexity | Schema changes are backward-compatible (e.g., new reset_frequency column). Risk: Low if following Laravel migration practices. |
| Testing overhead | Package includes tests; requires unit/integration tests for custom limit logic. Risk: Medium if limits are business-critical (e.g., billing). |
incrementBy()/decrementBy() but may need middleware for real-time updates.User, Account, or Project models needing quota enforcement.useLimit() in job handlers).@limit directives or manual checks in controllers.projects, api_calls) and plans (e.g., free, pro).Team) to test:
Limit::create()).user->setLimit()).checkApiQuota() for user->hasEnoughLimit('api_calls')).@limit directives or remainingLimit() reports.limit:reset with appropriate frequency (e.g., everyMinute for API limits).| Component | Compatibility |
|---|---|
| Laravel | ✅ v12+, v13 (tested). Avoid v11 or below. |
| PHP | ✅ 8.1+ (Laravel 12/13 requirement). |
| Databases | ✅ MySQL, PostgreSQL, SQLite (via Laravel migrations). |
| Caching | ✅ File, Redis, Memcached (configurable). |
| Queues | ✅ Supports async limit consumption (e.g., in jobs). |
| Blade | ✅ @limit directives for UI. |
| Testing | ✅ PHPUnit tests included; requires custom tests for business logic. |
composer require nabilhassen/laravel-usage-limiter
php artisan vendor:publish --provider="NabilHassen\LaravelUsageLimiter\ServiceProvider"
php artisan migrate
config/limit.php (e.g., cache store, model names).app/Console/Kernel.php:
$schedule->command('limit:reset')->everyMinute(); // Laravel 10+
HasLimits trait to target models (e.g., User.php).php artisan limit:create --name=projects --allowed_amount=5 --plan=standard --reset_frequency="every month"
created) or use middleware:
// In a controller or event listener
$user->useLimit('projects', 'standard');
@limit directives:
@limit($user, 'projects', 'standard')
<p>You have {{ $user->remainingLimit('projects', 'standard') }} projects left.</p>
@else
<p>Upgrade to add more projects!</p>
@endlimit
user->hasEnoughLimit().user->remainingLimit().limit:create commands).limit.php require cache clearing (php artisan config:clear).How can I help you explore Laravel packages today?