PHP code example of bassem / laravel-subscriptions

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

    

bassem / laravel-subscriptions example snippets


namespace App\Models;

use Rinvex\Subscriptions\Traits\HasSubscriptions;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasSubscriptions;
}

$plan = app('rinvex.subscriptions.plan')->create([
    'name' => 'Pro',
    'description' => 'Pro plan',
    'price' => 9.99,
    'signup_fee' => 1.99,
    'invoice_period' => 1,
    'invoice_interval' => 'month',
    'trial_period' => 15,
    'trial_interval' => 'day',
    'sort_order' => 1,
    'currency' => 'USD',
]);

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

$plan = app('rinvex.subscriptions.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 the plan instance to get feature's value
$amountOfPictures = $plan->getFeatureByName('pictures_per_listing')->value;

// Query the feature itself directly
$amountOfPictures = app('rinvex.subscriptions.plan_feature')->where('name', 'pictures_per_listing')->first()->value;

// Get feature value through the subscription instance
$amountOfPictures = app('rinvex.subscriptions.plan_subscription')->find(1)->getFeatureValue('pictures_per_listing');

$user = User::find(1);
$plan = app('rinvex.subscriptions.plan')->find(1);

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

$plan = app('rinvex.subscriptions.plan')->find(2);
$subscription = app('rinvex.subscriptions.plan_subscription')->find(1);

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

// Find plan feature
$feature = app('rinvex.subscriptions.plan_feature')->where('name', 'listing_duration_days')->first();

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

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

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

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

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

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

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

$user->subscribedTo($planId);

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

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

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

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

// Get subscriptions by plan
$subscriptions = app('rinvex.subscriptions.plan_subscription')->byPlanId($plan_id)->get();

// Get bookings of the given user
$user = \App\Models\User::find(1);
$bookingsOfUser = app('rinvex.subscriptions.plan_subscription')->ofUser($user)->get(); 

// Get subscriptions with trial ending in 3 days
$subscriptions = app('rinvex.subscriptions.plan_subscription')->findEndingTrial(3)->get();

// Get subscriptions with ended trial
$subscriptions = app('rinvex.subscriptions.plan_subscription')->findEndedTrial()->get();

// Get subscriptions with period ending in 3 days
$subscriptions = app('rinvex.subscriptions.plan_subscription')->findEndingPeriod(3)->get();

// Get subscriptions with ended period
$subscriptions = app('rinvex.subscriptions.plan_subscription')->findEndedPeriod()->get();

Rinvex\Subscriptions\Models\Plan;
Rinvex\Subscriptions\Models\PlanFeature;
Rinvex\Subscriptions\Models\PlanSubscription;
Rinvex\Subscriptions\Models\PlanSubscriptionUsage;
shell
    php artisan rinvex:publish:subscriptions
    
shell
    php artisan rinvex:migrate:subscriptions