- How do I set up usage limits for API calls per minute in Laravel?
- First, add the `HasLimits` trait to your User model. Then create a limit via `Limit::create(['name' => 'api_calls', 'allowed_amount' => 100, 'reset_frequency' => 'minute'])` and call `user->useLimit('api_calls')` before each API call. Schedule the `limit:reset` Artisan command in your `Kernel.php` to run every minute.
- Can this package handle multi-tier pricing (e.g., free, pro, enterprise) with different limits?
- Yes. Define separate limits for each plan (e.g., `projects_free`, `projects_pro`) and attach them to users via the `Limit` model. The package automatically checks remaining allowances per limit name, so users on different plans won’t interfere with each other.
- What happens if two users try to consume a limit simultaneously?
- The package uses database transactions for `useLimit()` and `unuseLimit()`, ensuring atomic operations. This prevents race conditions where two users might exceed their quota simultaneously. No manual locking is required.
- Does this work with Laravel 13, and what’s the migration path from Laravel 12?
- Yes, it’s fully compatible with Laravel 13 (v2.0.0+). For Laravel 12, use v1.1.0+. No breaking changes exist between versions. Run `composer update nabilhassen/laravel-usage-limiter` and update your `Kernel.php` to use `everySecond()` if needed.
- How do I backfill existing usage data for users who already have limits?
- Manually insert records into the `limit_usage` table for each user and limit combination. Use a data migration or script to populate `model_id`, `limit_id`, and `used_amount` fields. The package will then respect these values for future checks.
- Can I customize the cache store or TTL for performance?
- Yes. Configure the cache store and TTL in `config/usage-limiter.php`. The default is `cache` with a 24-hour TTL for limits and in-memory caching for model-specific usage. Adjust `cache_store` and `cache_ttl` as needed for your environment.
- What’s the best way to audit usage limits for compliance or billing?
- Use the `limitUsageReport()` method to generate reports for a specific model (e.g., `User::find(1)->limitUsageReport()`). For custom audits, query the `limit_usage` table directly or extend the package by adding event listeners for limit consumption.
- Will this work with polymorphic relationships (e.g., a `Usage` model for multiple limit types)?
- The `HasLimits` trait is designed for standard Eloquent models. If you need polymorphic support, ensure your model’s relationships don’t conflict with the package’s default table names (`limits`, `limit_usage`). Test with `morphMap` if using custom keys.
- How do I handle plan upgrades/downgrades without losing usage data?
- The package preserves usage data per limit name. When upgrading a user, create new limits for their plan (e.g., `projects_pro`) and let them consume those. Downgrades will enforce the new plan’s stricter limits automatically. No manual data cleanup is needed.
- Are there alternatives if I need per-millisecond API throttling?
- This package supports `everySecond()` for Laravel 10+, but ultra-high-frequency throttling (e.g., per-millisecond) may require a dedicated rate-limiting solution like `spatie/laravel-rate-limiter` or Redis-based tools like `predis/predis` with Lua scripts.