PHP code example of nabilhassen / laravel-usage-limiter

1. Go to this page and download the library: Download nabilhassen/laravel-usage-limiter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nabilhassen / laravel-usage-limiter example snippets


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

$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);

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

// app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
    ...
    // Laravel < 10
    $schedule->command('limit:reset')->everyMinute();

    // Laravel >= 10
    $schedule->command('limit:reset')->everySecond();
    ...
}

app()->make(\NabilHassen\LaravelUsageLimiter\LimitManager::class)->flushCache();
bash
php artisan vendor:publish --provider="NabilHassen\LaravelUsageLimiter\ServiceProvider"
bash
php artisan migrate
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
bash
php artisan limit:cache-reset