PHPackages                             nabilhassen/laravel-usage-limiter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. nabilhassen/laravel-usage-limiter

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

nabilhassen/laravel-usage-limiter
=================================

A laravel package to manage and limit usages/seats by plan, users, or other models

v1.1.0(1y ago)1803.2k↓39.6%13[1 PRs](https://github.com/nabilhassen/laravel-usage-limiter/pulls)MITPHPPHP ^8.0CI passing

Since Jun 14Pushed 1y ago3 watchersCompare

[ Source](https://github.com/nabilhassen/laravel-usage-limiter)[ Packagist](https://packagist.org/packages/nabilhassen/laravel-usage-limiter)[ Docs](https://github.com/nabilhassen/laravel-usage-limiter)[ RSS](/packages/nabilhassen-laravel-usage-limiter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (7)Versions (3)Used By (0)

Laravel Usage Limiter
=====================

[](#laravel-usage-limiter)

[![Tests](https://github.com/NabilHassen/laravel-usage-limiter/actions/workflows/tests.yml/badge.svg)](https://github.com/NabilHassen/laravel-usage-limiter/actions/workflows/tests.yml)

Introduction
------------

[](#introduction)

A Laravel package to track, limit, &amp; restrict usages of users, accounts, or any other model.

Features
--------

[](#features)

- Define usage limits you need for your app per plan
- Set reset frequency for each of your limits
- Attach usage limits to models (e.g. User model)
- Consume and unconsume usage limits whenever a resource is created or deleted
- Get usage report of a specific model (e.g. User model)
- Check and determine if a model can consume more resources
- Manually reset consumed usage limits of a model
- Automatically reset consumed usage limits by setting reset frequencies such as every second, every minute, every hour, every day, every month, etc and scheduling a built-in artisan command

Use cases
---------

[](#use-cases)

Basically with this package you can track your users' or any other models' usages and restrict them when they hit their maximum limits.

#### Example use-cases:

[](#example-use-cases)

- API usages per second, minute, month, etc
- Resource creation. E.g: projects, teams, users, products, etc
- Resource usages. E.g: storage, etc

Versions
--------

[](#versions)

Compatible for ***Laravel versions &gt;= 8.0***.

Quick Tutorial
--------------

[](#quick-tutorial)

This README documentation serves more as a reference. For a step-by-step getting started tutorial check  and you can always refer to this README documentation for details and advanced stuff.

Installation
------------

[](#installation)

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
```

Basic Usage
-----------

[](#basic-usage)

First, you need to use the **HasLimits** trait on your model.

```
use NabilHassen\LaravelUsageLimiter\Traits\HasLimits;

class User extends Authenticatable
{
    use HasLimits;
}
```

#### Create your Limits

[](#create-your-limits)

```
# 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);
```

###### Possible values for "reset\_frequency" column

[](#possible-values-for-reset_frequency-column)

- null
- "every second" // works in Laravel &gt;= 10
- "every minute"
- "every hour"
- "every day"
- "every week",
- "every two weeks",
- "every month",
- "every quarter",
- "every six months",
- "every year"

#### Set Limits on models

[](#set-limits-on-models)

```
$user->setLimit('projects', 'standard'); OR
$user->setLimit($projectsStandardLimit);
```

#### Set Limits on models with beginning used amounts

[](#set-limits-on-models-with-beginning-used-amounts)

If a user has already consumed limits then:

```
$user->setLimit('projects', 'standard', 2); OR
$user->setLimit($projectsStandardLimit, usedAmount: 2);
```

#### Unset Limits from models

[](#unset-limits-from-models)

```
$user->unsetLimit('projects', 'standard'); OR
$user->unsetLimit($projectsStandardLimit);
```

#### Consume/Unconsume Limits

[](#consumeunconsume-limits)

```
# 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*.
>
> [](#both-uselimit-and-unuselimit-methods-throws-an-exception-if-a-user-exceeded-limits-or-tried-to-unuse-limits-below-0)

#### Reset Limits for models

[](#reset-limits-for-models)

```
$user->resetLimit('projects', 'standard'); OR
$user->resetLimit($projectsStandardLimit);
```

#### All available methods

[](#all-available-methods)

MethodReturn TypeParameterssetLimittrue|throwstring|Limit $limit,
 ?string $plan = null,
 float|int $usedAmount = 0.0unsetLimitboolstring|Limit $limit,
 ?string $plan = nullisLimitSetboolstring|Limit $limit,
 ?string $plan = nulluseLimittrue|throwstring|Limit $limit,
 ?string $plan = null,
 float|int $amount = 1.0unuseLimittrue|throwstring|Limit $limit,
 ?string $plan = null,
 float|int $amount = 1.0resetLimitboolstring|Limit $limit,
 ?string $plan = nullhasEnoughLimitboolstring|Limit $limit,
 ?string $plan = nullusedLimitfloatstring|Limit $limit,
 ?string $plan = nullremainingLimitfloatstring|Limit $limit,
 ?string $plan = nulllimitUsageReportarraystring|Limit|null $limit = null,
 ?string $plan = null#### All available commands

[](#all-available-commands)

CommandArgumentsExamplelimit:createname: required
 allowed\_amount: required
 plan: optionalphp artisan limit:create --name products --allowed\_amount 20 --plan premiumlimit:deletename: required
 plan: optionalphp artisan limit:delete --name products --plan premiumlimit:listNonephp artisan limit:listlimit:resetNonephp artisan limit:reset # reset limit usages to 0limit:cache-resetNonephp artisan limit:cache-reset # flushes limits cache#### Blade

[](#blade)

```
# 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
```

Schedule Limit Usage Resetting
------------------------------

[](#schedule-limit-usage-resetting)

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();
    ...
}
```

Advanced Usage
--------------

[](#advanced-usage)

### Extending

[](#extending)

- If you would like to write your own model, make sure that your new Limit model extends `NabilHassen\LaravelUsageLimiter\Models\Limit::class` and change the model in the `limit.php` config file to your new model.
- If you already have used the `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.
- If there are any conficts in the database table names, you will just need to change the tables names in the `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.***

### Caching

[](#caching)

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.

- All of your Limits will be cached for 24 hours.
- On create, update, or deleting of a limit, the Limits cache will be refreshed.
- Model-specific limits are cached in-memory (i.e. during the request).

Manual Cache Reset
------------------

[](#manual-cache-reset)

In your code

```
app()->make(\NabilHassen\LaravelUsageLimiter\LimitManager::class)->flushCache();
```

Via command line

```
php artisan limit:cache-reset
```

Testing
-------

[](#testing)

```
composer test
```

Security
--------

[](#security)

If you have found any security issues, please send an email to the author at .

Contributing
------------

[](#contributing)

You are welcome to contribute to the package and you will be credited. Just make sure your PR does one thing and add tests.

License
-------

[](#license)

The Laravel Usage Limiter is open-sourced software licensed under the MIT license [MIT license](https://github.com/NabilHassen/laravel-usage-limiter/blob/main/LICENSE).

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance44

Moderate activity, may be stable

Popularity39

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 98.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~268 days

Total

2

Last Release

435d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/7040941f055a5267e8cc74be6107d2f90be4446ffe3b430adfb05f4e2aaec046?d=identicon)[NabilHassen](/maintainers/NabilHassen)

---

Top Contributors

[![nabilhassen](https://avatars.githubusercontent.com/u/58304777?v=4)](https://github.com/nabilhassen "nabilhassen (76 commits)")[![buchin](https://avatars.githubusercontent.com/u/156540?v=4)](https://github.com/buchin "buchin (1 commits)")

---

Tags

api-usagelaravelresource-usageresource-usage-trackingtrack-usageusageslaravelresource usagetrack usagelimit usagerestrict usageapi usagelimit accountplan limitsper usage

###  Code Quality

TestsPHPUnit

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/nabilhassen-laravel-usage-limiter/health.svg)

```
[![Health](https://phpackages.com/badges/nabilhassen-laravel-usage-limiter/health.svg)](https://phpackages.com/packages/nabilhassen-laravel-usage-limiter)
```

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[laravel/cashier

Laravel Cashier provides an expressive, fluent interface to Stripe's subscription billing services.

2.5k25.9M107](/packages/laravel-cashier)[laravel/pulse

Laravel Pulse is a real-time application performance monitoring tool and dashboard for your Laravel application.

1.7k12.1M99](/packages/laravel-pulse)[spatie/laravel-enum

Laravel Enum support

3655.4M31](/packages/spatie-laravel-enum)[flarum/core

Delightfully simple forum software.

211.3M1.9k](/packages/flarum-core)[zonneplan/laravel-module-loader

Module loader for Laravel

24118.4k](/packages/zonneplan-laravel-module-loader)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
