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']);
$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
$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
// 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);
});
});