PHP code example of elsayed85 / subscriptions

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

    

elsayed85 / subscriptions example snippets


namespace App\Models;

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

class User extends Authenticatable
{
    use HasSubscriptions;
}

$plan = app('elsayed85.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('elsayed85.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('elsayed85.subscriptions.plan_feature')->where('name', 'pictures_per_listing')->first()->value;

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

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

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

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

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

// Find plan feature
$feature = app('elsayed85.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('elsayed85.subscriptions.plan_subscription')->byPlanId($plan_id)->get();

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

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

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

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

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

elsayed85\Subscriptions\Models\Plan;
elsayed85\Subscriptions\Models\PlanFeature;
elsayed85\Subscriptions\Models\PlanSubscription;
elsayed85\Subscriptions\Models\PlanSubscriptionUsage;
shell
    php artisan vendor:publish --tag=tenant_subscriptions-config
    
shell
    php artisan elsayed85:publish-migration --to=landlord