Manage Subscription Plans and Entitlements in Laravel with Laravel Entitlements

最后更新于 经过

Manage Subscription Plans and Entitlements in Laravel with Laravel Entitlements image

Laravel Entitlements tackles one of the more involved problems in SaaS development: modeling what a subscriber is allowed to do, how much of it they can use, and what happens when they hit a limit or switch plans.

Rather than rolling your own entitlement logic, this package gives you a structured approach built around plans, licenses, and consumption tracking. Here's a quick overview of what the package covers:

  • Slot-based and pool-based consumption — slot strategies track one usage per subject (e.g., a specific device or seat), while pool strategies drain a shared counter (e.g., AI tokens or API credits)
  • Polymorphic ownership — any Eloquent model can act as a subscriber by adding the HasEntitlements trait, making it straightforward to scope entitlements to teams, workspaces, or individual users
  • Plan catalog with billing options — plans support monthly/yearly billing cycles and can be configured as recurring or fixed-term, with items that allow per-assignment quantity overrides
  • Domain events — the package dispatches events like LicenseConsumed , ReleaseRequested , 和 LicenseReconciled , giving you integration points for billing webhooks, background jobs, or audit logging
  • Optional Filament v5 admin panel — a ready-made management interface for plans, categories, and licenses that can be dropped into an existing Filament project

Defining Entitlement Types

The package uses a PHP backed enum to define what kinds of entitlements exist in your app and which strategy each one uses. This means the consumption logic is tied to your domain vocabulary rather than generic configuration:

枚举 LicenseType : 细绳 实现 EntitlementType
{
案件 Device = 'device' ;
案件 AiTokens = 'ai_tokens' ;
案件 Seat = 'seat' ;
民众 功能 战略 () : EntitlementStrategy
{
返回 匹配 $this ){
自己:: Device => 新的 SlotStrategy twoPhase : 真的 ),
自己:: AiTokens => 新的 PoolStrategy (),
自己:: Seat => 新的 SlotStrategy (),
};
}
}

twoPhase: true option on the Device case is worth noting — it splits release into two steps ( requestRelease() 然后 confirmRelease() ), which is useful when you need an external process (like revoking a device token) to complete before the slot is freed.

Consuming and Checking Entitlements

Once a plan is assigned to a subscriber, consuming an entitlement is a single method call:

// Slot-based: tie usage to a specific subject
Entitlements :: consume ($workspace, LicenseType :: Seat ,$user);
// Pool-based: drain a set amount from the shared counter
Entitlements :: consume ($workspace, LicenseType :: AiTokens , $usage, 数量 : 1500 (英文):

Checking availability before consuming is just as direct:

Entitlements :: ($workspace, LicenseType :: AiTokens , 1500 (英文): // 布尔值
Entitlements :: 可用的 ($workspace, LicenseType :: AiTokens (英文): // remaining capacity

When capacity runs out, the package throws a NoEntitlementAvailableException , which you can catch and handle at the controller or middleware level.

Plan Transitions

Switching a subscriber between plans is handled through changePlan() , which supports both immediate switches and end-of-period transitions. The package validates the transition before persisting it, so subscribers won't lose capacity for usages already in flight when a downgrade is applied.

安装

通过 Composer 安装:

作曲家 要求 masterix21/laravel-entitlements

Then publish and run the migrations:

php 工匠 供应商:发布 --标签= "laravel-entitlements-config"
php 工匠 供应商:发布 --标签= "laravel-entitlements-migrations"
php 工匠 迁移

The package requires PHP 8.2+ and Laravel 11 or higher. The optional Filament integration requires Filament v5.

You can find the full documentation and source code on GitHub

保罗·雷德蒙德照片

Laravel News 特约撰稿人。全栈 Web 开发人员兼作家。

归档于:
立方体

Laravel 时事通讯

加入超过 4 万名开发者的行列,不错过任何新的技巧、教程等内容。

图像
Laravel 代码审查

几天内即可获得 Laravel 代码审查方面的专家指导

访问 Laravel 代码审查
Laravel Cloud 标志

Laravel 云

轻松创建和管理服务器,并在几秒钟内部署 Laravel 应用程序。

Laravel 云
SaaSykit:Laravel SaaS 入门套件徽标

SaaSykit:Laravel SaaS 入门套件

SaaSykit 是一个多租户 Laravel SaaS 入门套件,包含运行现代 SaaS 所需的所有功能,例如支付、美观的结账界面、管理面板、用户仪表盘、身份验证、现成组件、统计数据、博客、文档等等。

SaaSykit:Laravel SaaS 入门套件
Kirschbaum 标志

樱桃树

提供创新和稳定性,确保您的Web应用程序取得成功。

樱桃树
鱼叉:新一代时间跟踪和发票标志

Harpoon:新一代时间跟踪和发票系统

新一代时间跟踪和计费软件,帮助您的机构规划和预测盈利的未来。

Harpoon:新一代时间跟踪和发票系统
绝不妥协标志

绝不妥协

来自 No Compromises 播客的两位经验丰富的开发者 Joel 和 Aaron 现在可以为您的 Laravel 项目提供服务。⬧ 固定费用 9500 美元/月。⬧ 无冗长的销售流程。⬧ 无需签订合同。⬧ 100% 退款保证。

绝不妥协
PhpStorm 标志

PhpStorm

首选的 PHP IDE,对 Laravel 及其生态系统提供广泛的开箱即用支持。

PhpStorm
了解 Softtech 的标志

了解软科技

Acquaint Softtech 提供 AI 就绪的 Laravel 开发人员,48 小时内即可上手,每月费用为 3000 美元,没有冗长的销售流程,并提供 100% 退款保证。

了解软科技
Shift 标志

转移

还在运行旧版本的 Laravel?立即实现 Laravel 自动升级和代码现代化,让您的应用程序保持最新状态。

转移
Tinkerwell 徽标

廷克威尔

Laravel 开发者必备的代码运行器。可在本地和生产环境中体验 AI、自动补全和即时反馈功能。

廷克威尔
Lucky Media 标志

幸运传媒

Get Lucky Now——拥有十余年经验的 Laravel 开发理想之选!

幸运传媒
Laracon AU 2026 Announces Full Speaker Lineup, Schedule, and Workshops image

Laracon AU 2026 Announces Full Speaker Lineup, Schedule, and Workshops

阅读文章
Parsel: Parse PDFs, Office Documents, and Images in PHP image

Parsel: Parse PDFs, Office Documents, and Images in PHP

阅读文章
Typed Objects for Eloquent with Expressive image

Typed Objects for Eloquent with Expressive

阅读文章
Malware Blocking and Dependency Policies in Composer 2.10 image

Malware Blocking and Dependency Policies in Composer 2.10

阅读文章
Aegis for Laravel: Scaffolding and Validation Helpers for Value Objects image

Aegis for Laravel: Scaffolding and Validation Helpers for Value Objects

阅读文章
Playa: Cookie-Based Temporary Players for Laravel image

Playa: Cookie-Based Temporary Players for Laravel

阅读文章