PHP code example of laravelcm / laravel-subscriptions

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

    

laravelcm / laravel-subscriptions example snippets


namespace App\Models;

use Laravelcm\Subscriptions\Traits\HasPlanSubscriptions;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasPlanSubscriptions;
}

use Laravelcm\Subscriptions\Models\Plan;
use Laravelcm\Subscriptions\Models\Feature;
use Laravelcm\Subscriptions\Interval;

$plan = Plan::create([
    'name' => 'Pro',
    'description' => 'Pro plan',
    'price' => 9.99,
    'signup_fee' => 1.99,
    'invoice_period' => 1,
    'invoice_interval' => Interval::MONTH->value,
    'trial_period' => 15,
    'trial_interval' => Interval::DAY->value,
    'sort_order' => 1,
    'currency' => 'USD',
]);

// Create multiple plan features at once
$plan->features()->saveMany([
    new Feature(['name' => 'listings', 'value' => 50, 'sort_order' => 1]),
    new Feature(['name' => 'pictures_per_listing', 'value' => 10, 'sort_order' => 5]),
    new Feature(['name' => 'listing_duration_days', 'value' => 30, 'sort_order' => 10, 'resettable_period' => 1, 'resettable_interval' => 'month']),
    new Feature(['name' => 'listing_title_bold', 'value' => 'Y', 'sort_order' => 15])
]);

use Laravelcm\Subscriptions\Models\Plan;

$plan = Plan::find(1);

// 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 Laravelcm\Subscriptions\Models\Feature;
use Laravelcm\Subscriptions\Models\Subscription;

// Use the plan instance to get feature's value
$amountOfPictures = $plan->getFeatureBySlug('pictures_per_listing')->value;

// Query the feature itself directly
$amountOfPictures = Feature::where('slug', 'pictures_per_listing')->first()->value;

// Get feature value through the subscription instance
$amountOfPictures = Subscription::find(1)->getFeatureValue('pictures_per_listing');

use Laravelcm\Subscriptions\Models\Plan;
use App\Models\User;

$user = User::find(1);
$plan = Plan::find(1);

$user->newPlanSubscription('main', $plan);

use Laravelcm\Subscriptions\Models\Plan;
use Laravelcm\Subscriptions\Models\Subscription;

$plan = Plan::find(2);
$subscription = Subscription::find(1);

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

use Laravelcm\Subscriptions\Models\Feature;

// Find plan feature
$feature = Feature::where('name', 'listing_duration_days')->first();

// Get feature reset date
$feature->getResetDate(new \Carbon\Carbon());

$user->planSubscription('main')->canUseFeature('listings');

$user->planSubscription('main')->recordFeatureUsage('listings');

// Increment by 2
$user->planSubscription('main')->recordFeatureUsage('listings', 2);

// Override with 9
$user->planSubscription('main')->recordFeatureUsage('listings', 9, false);

$user->planSubscription('main')->reduceFeatureUsage('listings', 2);

$user->planSubscription('main')->usage()->delete();

$user->subscribedTo($planId);

$user->planSubscription('main')->active();
$user->planSubscription('main')->canceled();
$user->planSubscription('main')->ended();
$user->planSubscription('main')->onTrial();

$user->planSubscription('main')->renew();

$user->planSubscription('main')->cancel();

$user->planSubscription('main')->cancel(true);

use Laravelcm\Subscriptions\Models\Subscription;
use App\Models\User;

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

// Get bookings of the given user
$user = User::find(1);
$bookingsOfSubscriber = Subscription::ofSubscriber($user)->get(); 

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

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

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

// Get subscriptions with ended period
$subscriptions = Subscription::findEndedPeriod()->get();

Laravelcm\Subscriptions\Models\Plan;
Laravelcm\Subscriptions\Models\Feature;
Laravelcm\Subscriptions\Models\Subscription;
Laravelcm\Subscriptions\Models\SubscriptionUsage;
shell
    php artisan vendor:publish --provider="Laravelcm\Subscriptions\SubscriptionServiceProvider"
    
shell
    php artisan migrate