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.
A Laravel package to track, limit, & restrict usages of users, accounts, or any other model.
Basically with this package you can track your users' or any other models' usages and restrict them when they hit their maximum limits.
Compatible for Laravel versions >= 8.0.
This README documentation serves more as a reference. For a step-by-step getting started tutorial check https://nabilhassen.com/laravel-usage-limiter-manage-rate-and-usage-limits and you can always refer to this README documentation for details and advanced stuff.
Install Laravel Usage Limiter using the Composer package manager:
composer require nabilhassen/laravel-usage-limiter
Next, you should publish the Laravel Usage Limiter configuration and migration files using the vendor:publish Artisan command:
php artisan vendor:publish --provider="NabilHassen\LaravelUsageLimiter\ServiceProvider"
Finally, you should run the migrate command in order to create the tables needed to store Laravel Usage Limiter's data:
php artisan migrate
First, you need to use the HasLimits trait on your model.
use NabilHassen\LaravelUsageLimiter\Traits\HasLimits;
class User extends Authenticatable
{
use HasLimits;
}
# On standard plan 5 projects are allowed per month
$projectsStandardLimit = Limit::create([
'name' => 'projects',
'allowed_amount' => 5,
'plan' => 'standard', // optional
'reset_frequency' => 'every month' // optional
]);
# On pro plan 10 projects are allowed per month
$projectsProLimit = Limit::create([
'name' => 'projects',
'allowed_amount' => 10,
'plan' => 'pro', // optional
'reset_frequency' => 'every month' // optional
]);
# Increment projects limit on standard plan from 5 to 15 per month
$projectsStandardLimit->incrementBy(10);
# Decrement projects limit on pro plan from 10 to 7 per month
$projectsProLimit->decrementBy(3);
$user->setLimit('projects', 'standard'); OR
$user->setLimit($projectsStandardLimit);
If a user has already consumed limits then:
$user->setLimit('projects', 'standard', 2); OR
$user->setLimit($projectsStandardLimit, usedAmount: 2);
$user->unsetLimit('projects', 'standard'); OR
$user->unsetLimit($projectsStandardLimit);
# When a user creates a project
$user->useLimit('projects', 'standard'); OR
$user->useLimit($projectsStandardLimit);
# When a user creates multiple projects
$user->useLimit('projects', 'standard', 3); OR
$user->useLimit($projectsStandardLimit, amount: 3);
# When a user deletes a project
$user->unuseLimit('projects', 'standard'); OR
$user->unuseLimit($projectsStandardLimit);
# When a user deletes multiple projects
$user->unuseLimit('projects', 'standard', 3); OR
$user->unuseLimit($projectsStandardLimit, amount: 3);
Both useLimit and unuseLimit methods throws an exception if a user exceeded limits or tried to unuse limits below 0.
$user->resetLimit('projects', 'standard'); OR
$user->resetLimit($projectsStandardLimit);
| Method | Return Type | Parameters |
|---|---|---|
| setLimit | true|throw | string|Limit $limit, ?string $plan = null, float|int $usedAmount = 0.0 |
| unsetLimit | bool | string|Limit $limit, ?string $plan = null |
| isLimitSet | bool | string|Limit $limit, ?string $plan = null |
| useLimit | true|throw | string|Limit $limit, ?string $plan = null, float|int $amount = 1.0 |
| unuseLimit | true|throw | string|Limit $limit, ?string $plan = null, float|int $amount = 1.0 |
| resetLimit | bool | string|Limit $limit, ?string $plan = null |
| hasEnoughLimit | bool | string|Limit $limit, ?string $plan = null |
| usedLimit | float | string|Limit $limit, ?string $plan = null |
| remainingLimit | float | string|Limit $limit, ?string $plan = null |
| limitUsageReport | array | string|Limit|null $limit = null, ?string $plan = null |
| Command | Arguments | Example |
|---|---|---|
| limit:create | name: required allowed_amount: required plan: optional | php artisan limit:create --name products --allowed_amount 20 --plan premium |
| limit:delete | name: required plan: optional | php artisan limit:delete --name products --plan premium |
| limit:list | None | php artisan limit:list |
| limit:reset | None | php artisan limit:reset # reset limit usages to 0 |
| limit:cache-reset | None | php artisan limit:cache-reset # flushes limits cache |
# Using limit instance
@limit($user, $projectsStandardLimit)
// user has enough limits left
@else
// user has NO enough limits left
@endlimit
# Using limit name and plan
@limit($user, 'projects', 'standard')
// user has enough limits left
@else
// user has NO enough limits left
@endlimit
The limit:reset command will reset your model's (e.g. user) limit usages based on the Limit's reset_frequency.
Add limit:reset command to the console kernel.
// app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
...
// Laravel < 10
$schedule->command('limit:reset')->everyMinute();
// Laravel >= 10
$schedule->command('limit:reset')->everySecond();
...
}
NabilHassen\LaravelUsageLimiter\Models\Limit::class and change the model in the limit.php config file to your new model.limits method or property where the HasLimits trait is used, you can change it to any string value (e.g. 'restricts') by changing the relationship key in the limit.php config file and you're done.limit.php config file and you're good to go.Clear your config cache if you have made any changes in the limit.php config file.
By default, Laravel Usage Limiter uses the default cache you chose for your app. If you would like to use any other cache store you will need to change the store key in the limit.php config file to your preferred cache store.
In your code
app()->make(\NabilHassen\LaravelUsageLimiter\LimitManager::class)->flushCache();
Via command line
php artisan limit:cache-reset
composer test
If you have found any security issues, please send an email to the author at hello@nabilhassen.com.
You are welcome to contribute to the package and you will be credited. Just make sure your PR does one thing and add tests.
The Laravel Usage Limiter is open-sourced software licensed under the MIT license MIT license.
How can I help you explore Laravel packages today?