PHP code example of saadmajeed / laravel-entitlements

1. Go to this page and download the library: Download saadmajeed/laravel-entitlements 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/ */

    

saadmajeed / laravel-entitlements example snippets


use SaadMajeed\Entitlements\Traits\HasPlan;

class User extends Authenticatable
{
    use HasPlan;
}

use SaadMajeed\Entitlements\Models\Plan;

$plan = Plan::create(['name' => 'Pro', 'slug' => 'pro']);

$user->plan()->associate($plan);
$user->save();

use SaadMajeed\Entitlements\Models\Entitlement;

// Boolean feature flag
Entitlement::create([
    'key' => 'sso',
    'type' => 'boolean',
    'default_value' => false,
]);

// Numeric limit
Entitlement::create([
    'key' => 'max_projects',
    'type' => 'limit',
    'default_value' => 3,
]);

// Metered usage (resets monthly)
Entitlement::create([
    'key' => 'api_calls',
    'type' => 'metered',
    'default_value' => 1000,
    'reset_period' => 'monthly',
]);

// Time-based feature
Entitlement::create([
    'key' => 'early_access',
    'type' => 'time',
    'metadata' => ['expires_at' => '2025-12-31'],
]);

// Conditional feature
Entitlement::create([
    'key' => 'beta_feature',
    'type' => 'conditional',
    'metadata' => [
        'conditions' => [
            ['field' => 'region', 'operator' => '=', 'value' => 'us'],
        ],
    ],
]);

$plan->entitlements()->attach($entitlement->id, [
    'value' => ['enabled' => true],  // boolean
    // or 'value' => 10,             // numeric limit
    // or 'value' => ['max' => 50],  // alternative limit syntax
]);

use SaadMajeed\Entitlements\Facades\Entitlements;

Entitlements::plan('base');

Entitlements::plan('pro')
    ->feature('sso', true)                                  // boolean flag
    ->limit('max_projects', 50)                             // numeric cap
    ->meter('api_calls', 1000, 'monthly')                   // metered, resets monthly
    ->conditional('beta', [                                 // gated by subject attributes
        ['field' => 'role', 'operator' => '=', 'value' => 'beta'],
    ])
    ->time('trial', '2025-12-31')                           // expires on a date
    ->parent('base');                                       // inherit from base

Entitlements::plan('pro', function ($plan) {
    $plan->feature('sso')->limit('max_projects', 50);
});

Entitlements::plan('pro')
    ->remove('legacy_feature')          // drop a single entitlement
    ->removeMany(['old_a', 'old_b']);   // drop several at once

// Make the plan authoritative — keep only these, detach everything else
// (existing pivot values for the kept entitlements are preserved):
Entitlements::plan('pro')->only(['sso', 'max_projects', 'api_calls']);

// Clear plan inheritance:
Entitlements::plan('pro')->withoutParent();

// Via trait
$user->can('sso');                    // bool
$user->limit('max_projects');         // ?float
$user->remaining('api_calls');        // ?float
$user->use('api_calls', 1);           // Decision (record usage)
$user->consume('api_calls', 1);       // alias of use()
$user->value('sso');                  // resolved value (e.g. ['enabled' => true])
$user->why('sso');                    // Explanation

// Via facade
Entitlement::for($user)->can('sso');
Entitlement::for($user)->consume('api_calls', 1);
Entitlement::for($user)->why('sso');

// Via helper
entitlement($user)->can('sso');

$user->can('sso'); // true or false

$user->limit('max_projects');     // e.g. 5
$user->remaining('max_projects'); // e.g. 3 (if 2 used)

$user->remaining('api_calls');          // e.g. 950 (of 1000)
$user->use('api_calls', 1);             // record usage, returns Decision
$user->use('api_calls', 5);             // record multiple at once

Entitlement::create([
    'key' => 'bonus_features',
    'type' => 'metered',
    'default_value' => 100,
    'reset_period' => 'monthly',
    'metadata' => ['soft_limit' => true],
]);

$user->can('early_access'); // false if expired, true otherwise

// metadata: ['conditions' => [['field' => 'role', 'operator' => '=', 'value' => 'admin']]]
$user->can('beta_feature'); // true only for admins

$explanation = entitlement($user)->why('max_upload_size');
$value = $explanation->value; // the raw configured value

$base = Plan::create(['name' => 'Base', 'slug' => 'base']);
$pro = Plan::create(['name' => 'Pro', 'slug' => 'pro', 'parent_id' => $base->id]);

// Pro inherits all entitlements from Base,
// plus any values explicitly set on Pro

use SaadMajeed\Entitlements\Models\EntitlementOverride;

EntitlementOverride::create([
    'subject_type' => $user->getMorphClass(),
    'subject_id' => $user->getKey(),
    'entitlement_id' => $entitlement->id,
    'value' => ['enabled' => true],
    'expires_at' => now()->addDays(7),
    'reason' => 'Granted temporary access',
    'performed_by_type' => $admin->getMorphClass(),
    'performed_by_id' => $admin->getKey(),
]);

Entitlement::for($user)->flushCache();       // all entitlements for user
Entitlement::for($user)->flushCache('sso');  // specific key

$decision = Entitlement::for($user)->use('api_calls', 1);
$decision->remaining; // 999 (of 1000)

// In routes/web.php
Route::get('/sso', function () {
    return view('sso');
})->middleware('entitlement:sso');

// With a redirect
Route::get('/beta', function () {
    return view('beta');
})->middleware('entitlement:beta_feature:/upgrade');

// In bootstrap/app.php or ExceptionHandler
->withExceptions(function (Exceptions $exceptions) {
    $exceptions->render(function (LimitExceededException $e) {
        return response()->json(['error' => $e->getMessage()], 429);
    });
});
bash
php artisan vendor:publish --tag=entitlements-config
php artisan vendor:publish --tag=entitlements-migrations
php artisan migrate
bash
php artisan entitlement:install
bash
php artisan entitlement:cache-warm