Subscriptions
Manage subscriptions on-premises, without any payment systems!
use Illuminate\Support\Facades\Auth;
use Laragear\Subscriptions\Models\Plan;
$subscription = Auth::user()->subscribeTo(Plan::find(1));
return "You are now subscribed to $subscription->name!";

Become a Sponsor and get instant access to this package.
Requirements
- PHP 8.4 or later
- Laravel 12 or later.
Installation
You can install the package via Composer. Open your composer.json and point the location of the private repository under the repositories key.
{
// ...
"repositories": [
{
"type": "vcs",
"name": "laragear/subscriptions",
"url": "https://github.com/laragear/subscriptions.git"
}
],
}
Then call Composer to retrieve the package.
composer require laragear/subscriptions
You will be prompted for a personal access token. If you don't have one, follow the instructions or create one here. It takes just seconds.
How does this work?
Laragear's Subscriptions package uses the power of Laravel Eloquent Models with some magic to handle a small but flexible Subscription system, detached from any payment service (Stripe, Paypal, Parcel, etc).
The basic approach is simple: a Subscription is created based on a Plan. Subscribers can manually renew their subscription, or you may do it automatically by saving their payment data.
This enables multiple ways to handle subscriptions across your app:
- Upgrade only to a group of Plans.
- Share a subscription between multiple users.
- Cancel or immediately terminate a subscription.
- Limit Plans based on a number of subscribers, or time.
- Lock and unlock Plans...
... and much more.
Important
This library does not handle payments. You're free to push your finances inside your application, or offload that into another application or service, especially if you want to charge automatically.
Set up
Before creating plans and subscribing users to a Plan, you will need to install the application and setup some models so you're ready to go. Don't worry, it only takes 5 minutes.
1. Install the files
Install the migrations tables, the policies, and the plans blueprints file. You can install everything with the convenient subscriptions::install Artisan command.
php artisan subscriptions:install
2. Set the relationship
Tip
If you will be subscribing only your App\Models\User model, and access them using subscribers(), you can skip this step.
To add your Users as subscribers, and be able to access them easily through the Subscription model, use Subscription::macroRelation(). For polymorphic relations, use Subscription::macroPolymorphicRelation(). You can do this in your App\Providers\AppServiceProvider class, or in your bootstrap/app.php file.
These are methods that use dynamic relationships but with a more complete relation callback and name => class convenience.
use Laragear\Subscriptions\Models\Subscription;
use Illuminate\Foundation\Application;
use App\Models\Adult;
use App\Models\Kid;
return Application::configure(basePath: dirname(__DIR__))
->booted(function () {
// For a simple monomorphic relationship.
Subscription::macroRelation('adults', Adult::class);
// For all of your polymorphic relationships.
Subscription::macroPolymorphicRelations([
'adults' => Adult::class,
'kids' => Kids::class,
]);
})->create();
This way you will be able to access subscribers from a Subscription model using the relation name:
use Laragear\Subscriptions\Models\Subscription;
$actors = Subscription::find('bc728326...')->adults;
3. Migrate the tables.
If you don't require any further changes to the migrations files, you can just migrate your database tables like it was another day in the office:
Tip
Migrations create 3 tables: plans, subscribable and subscriptions.
4. Add the WithSubscription trait
Finally, add the Laragear\Subscriptions\WithSubscriptions trait to your models, which will enable it to handle subscriptions easily with just a few methods.
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laragear\Subscriptions\WithSubscriptions;
class User extends Authenticatable
{
use WithSubscriptions;
// ...
}
Plans
First, we need to create the Plans. We can go inside the plans/plans.php and create them using the convenient Plan builder. You're free to remove the example Plan.
use Laragear\Subscriptions\Facades\Plan;
Plan::called('Free')->capability('coolness', 10)->monthly();
Tip
A Plan works like a blueprint for a Subscription: when a Subscription is created, the Plan data is copied into the Subscription.
For example, you may create two plans for a Delivery app: one for free, and the other paid. We will differentiate them by their names and the number of deliveries per month allowed.
use Laragear\Subscriptions\Facades\Plan;
Plan::called('Free')->capability('deliveries', 1)->monthly();
Plan::called('Basic')->capability('deliveries', 8)->monthly();
Warning
Plans require an interval. You will get an error if the interval is not set. You can easily make lifetime plans by renewing them automatically in your app.
Once plans are declared, we will need to push them into the database. For that, we can use subscriptions:plans. The command will read the plans and create them into the database.
php artisan subscriptions:plans
# Created 2 plans:
# - Free
# - Basic
That's it, from here you can subscribe entities to it, which can be users, companies, or whatever you want.
Before going into the subscriptions themselves, let's check how we can configure a Plan thoroughly.
Plan Capabilities
Capabilities are the core of Laragear's Subscriptions. Simple Plans may not need any, but complex ones may need a list of values to allow (or not) a user to do a given action.
The capabilities set in the Plan will get copied over new Subscriptions. This makes the Plan immutable, and make changes only affect new Subscriptions.
Capabilities can be set using capability(). For example, we will set the number of deliveries for the Basic Plan:
use Laragear\Subscriptions\Facades\Plan;
Plan::called('Basic')->capability('deliveries', 8)->monthly();
You can also set a list of capabilities in one go using an array.
use Laragear\Subscriptions\Facades\Plan;
Plan::called('Basic')->capability([
'deliveries' => 8,
'delivery' => [
'priority' => 'normal',
'groceries' => false,
]
])->monthly();
Capabilities can be accessed using dot.notation. In your Subscription, you can access them later with capability().
$priority = $user->subscription->capability('delivery.priority');
Metadata
Metadata is like a note in the plan itself, which are is visible at serialization by default. This metadata replicated into the subscription itself. To attach metadata to a plan use withMetadata().
use Laragear\Subscriptions\Facades\Plan;
Plan::called('Free')->withMetadata([
'payment' => 'not_allowed',
'remember' => 'weekly',
])->monthly();
Metadata is just a Collection instance, so it's easy to retrieve and save values to it later in the Subscription.
echo $subscription->metadata('payment'); // "not_allowed"
Custom columns
Since the migration files are editable with new columns, you can use withColumns() to add primitive values to their respective columns.
use Laragear\Subscriptions\Facades\Plan;
Plan::called('Basic')->withColumns([
'price' => 29.90,
])->monthly();
Later, these values can be accessed like a normal property from its Subscription, as these values will be copied over them as long the column exists.
echo $subscription->price; // 29.90
Plan groups and levels
Plan groups allows to Subscriptions to be upgraded only to Plans of the same group, and only for "better" plans. This makes a clear path to subscription upgrades, like from "Free" to "Basic" and vice versa.
With group() and the name on the group, you can add your Plans from the most basic to the more powerful.
use Laragear\Subscriptions\Facades\Plan;
Plan::group('deliveries', [
0 => Plan::called('Free')->capability('deliveries', 1)->monthly(),
1 => Plan::called('Basic')->capability('deliveries', 8)->monthly(),
]);
With that, users won't be able to downgrade to a lesser plan, or subscribe to two plans of the same group, when checking these actions with the authorization gates.
use Laragear\Subscriptions\Models\Plan;
use Illuminate\Support\Facades\Auth;
$plan = Plan::find('b6954722...');
if (Auth::user()->cannot('upgradeTo', $plan)) {
return "You cannot upgrade to the $plan->name.";
}
Auth::user()->upgradeTo($plan);
Since downgrades are disabled, you can enable them using downgradeableGroup() through the Plan Facade.