PHP code example of rabol / laravel-simple-subscription

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

    

rabol / laravel-simple-subscription example snippets


return [
];

namespace App\Models;

use Rabol\SimpleSubscription\Traits\HasSubscriptions;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasSubscriptions;
}

$newPlan = SimpleSubscriptionPlan::create([
    'name' => 'My cool subscription',
    'description' => 'This is a very cool plan',
    'is_active' => true,
    'price' => 12.50,
    'signup_fee' => 0,
    'currency' => 'eur',
    'trial_period'  => 1,
    'trial_interval' => 'week',
    'invoice_period' => 1,
    'invoice_interval' => 'month',
    'grace_period' => 3,
    'grace_interval' => 'day',
]);


$planFeature = $newPlan
    ->features()
    ->create([
        'name' => 'My cool feature',
        'description' => 'This is my cool feature',
        'value' => 100,
        'resettable_period' => 2,
        'resettable_interval' => 'month',
        'sort_order' => 1,
    ]);

$plan = SimpleSubscriptionPlan::find(1);
or
$plan = SimpleSubscriptionPlan::whereName('My cool subscription')->first();

// Get all plan features                
$plan->features;

// Get all plan subscriptions
$plan->subscriptions;

// Check if the plan is free
$plan->isFree();

// Check if the plan has trial period
$plan->hasTrial();

// Check if the plan has grace period
$plan->hasGrace();

// Use the plan instance to get feature's value
$myCoolFeatureValue = $plan->getFeatureByName('My Cool feature')->value;

// Get the usage of the feature you can
$myCoolFeatureUsage = SimpleSubscriptionPlanFeature::wherePlanId(1)->whereName('Feature 1')->first()->usage()->first()->used;

$user = User::find(1);
$plan = SimpleSubscriptionPlan::whereName('My cool subscription')->first();

$user->newSubscription($plan);

$user = User::find(1);
$plan = SimpleSubscriptionPlan::whereName('My cool subscription')->first();

$user->newSubscription($plan, Carbon::today());

$newPlan = SimpleSubscriptionPlan::whereName('My second cool plan')->first();
$subscription = SimpleSubscriptionPlanSubscription::whereName('My cool subscription')->first();

// Change subscription plan
$subscription->changePlan($newPlan);

$user->subscription('My cool subscription')->canUseFeature('My cool feature');

$user->subscription('My cool subscription')->recordFeatureUsage('My cool feature');

// Increment by 2
$user->subscription('My cool subscription')->recordFeatureUsage('My cool feature', 2);

// Override with 9
$user->subscription('My cool subscription')->recordFeatureUsage('My cool feature', 9, false);

$user->subscription('My cool subscription')->reduceFeatureUsage('My cool feature', 2);

$user->subscription('My cool subscription')->increaseFeatureUsage('My cool feature', 2);

$user->subscription('My cool subscription')->usage()->delete();

$user->subscribedToPlanId($planId);
or
$user->subscribedToPlanName('My cool plan');

$user->subscription('My cool subscription')->active();
$user->subscription('My cool subscription')->canceled();
$user->subscription('My cool subscription')->ended();
$user->subscription('My cool subscription')->onTrial();

$user->subscription('My cool subscription')->renew();

$user->subscription('My cool subscription')->cancel();

$user->subscription('My cool subscription')->cancel(true);

// Get subscriptions by plan
$subscriptions = SimpleSubscriptionPlanSubscription::byPlanId($plan_id)->get();

// Get bookings of the given user
$user = \App\Models\User::find(1);
$bookingsOfUser = SimpleSubscriptionPlanSubscription::ofSubscriber($user)->get();

// Get subscriptions with trial ending in 3 days
$subscriptions = SimpleSubscriptionPlanSubscription::findEndingTrial(3)->get();

// Get subscriptions with ended trial
$subscriptions = SimpleSubscriptionPlanSubscription::findEndedTrial()->get();

// Get subscriptions with period ending in 3 days
$subscriptions = SimpleSubscriptionPlanSubscription::findEndingPeriod(3)->get();

// Get subscriptions with ended period
$subscriptions = SimpleSubscriptionPlanSubscription::findEndedPeriod()->get();
bash
php artisan vendor:publish --provider="Rabol\SimpleSubscription\SimpleSubscriptionServiceProvider" --tag="laravel-simple-subscription-migrations"
php artisan migrate
bash
php artisan vendor:publish --provider="Rabol\SimpleSubscription\SimpleSubscriptionServiceProvider" --tag="laravel-simple-subscription-config"