PHP code example of rokde / laravel-subscription-manager
1. Go to this page and download the library: Download rokde/laravel-subscription-manager 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/ */
rokde / laravel-subscription-manager example snippets
// \App\Models\User
class User extends \Illuminate\Foundation\Auth\User {
use \Rokde\SubscriptionManager\Models\Concerns\Subscribable;
}
// using the SubscriptionBuilder, presented by Subscribable trait
/** @var \Rokde\SubscriptionManager\Models\Factory\SubscriptionBuilder $builder */
$builder = $user->newSubscription(); // without any plan
// or
$plan = \Rokde\SubscriptionManager\Models\Plan::byName('Superior');
$builder = $user->newSubscription($plan); // subscribing to a plan -> the list of features will be taken from the plan
// or
$builder = $user->newFeatureSubscription(['feature-1', 'feature-2']); // just a list of features, must not exist in database
$builder->periodLength('P1M') // set period length to a month (default is 1 year)
->infinitePeriod() // or set an infinite period
->trialDays(30) // set 30 days for trial
->skipTrial() // or skip trial (default)
->create(); // and create a subscription
$subscription->circles(); // resolves a list of SubscriptionCircles
$user->subscription->cancel(); // cancel at the end of the current circle or at the end of the trial when you are on a trial
// or
$user->subscription->cancelNow(); // cancel just right now
// or
$user->subscription->cancelAt($datetime); // cancel at a concrete time
$user->subscription->resume(); // resume a cancelled subscription within grace period
$meteredFeature = (new CreateFeatureAction())->execute('users-count', true);
$subscription = (new CreateSubscriptionFromFeaturesAction())
->execute([$meteredFeature], request()->user(), function (SubscriptionBuilder $builder) {
$builder->setQuota('users-count', 10); // allow 10 users
});
// /routes/web.php
Route::group(['middleware' => 'subscribed'], function () {
// here you can define your premium routes
});
// /routes/web.php
Route::group(['middleware' => 'subscribed:track-time'], function () {
// here you can define your routes for all users which have subscribed to a feature "track-time"
});
// @var \App\Models\User|\App\Models\Team $subscribable
// just currently active subscriptions
$hasAnyActiveSubscription = $subscribable->subscribed();
$isActivelySubscribedToAConcreteFeature = $subscribable->subscribed('feature-1');
// active and past subscriptions
$hasAnySubscriptionEver = $subscribable->everSubscribed();
$wasSubscribedToAConcreteFeature = $subscribable->everSubscribed('feature-1');
// \App\Models\Team
class Team extends \Laravel\Jetstream\Team {
use \Rokde\SubscriptionManager\Models\Concerns\Subscribable;
}
$history = new \Rokde\SubscriptionManager\Insights\SubscriptionHistory();
$histogram = $history->get(); // keyed-map
'2021-01-01' => [
'start' => '2021-01-01 00:00:00', // start of period
'end' => '2021-02-01 00:00:00', // end of period
'count' => 3, // number of subscriptions within the period
'new' => 2, // newly created subscriptions within the period
'trial' => 2, // number of subscriptions being on trial within the period
'grace' => 2, // number of subscriptions being on grace period within the period
'ended' => 2, // subscriptions finally ended within the period
],