PHP code example of oanhnn / laravel-pricing-plans

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

    

oanhnn / laravel-pricing-plans example snippets


// config/app.php

    'providers' => [
        // Other service providers...

        Laravel\PricingPlans\PricingPlansServiceProvider::class,
    ],



namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\PricingPlans\Contracts\Subscriber;
use Laravel\PricingPlans\Models\Concerns\Subscribable;

class User extends Authenticatable implements Subscriber
{
    use Subscribable;
    // ...
}



namespace App\Models;

use Laravel\PricingPlans\Models\Feature as Model;

class Feature extends Model
{
    const FEATURE_UPLOAD_IMAGES = 'upload-images';
    const FEATURE_UPLOAD_VIDEO = 'upload-video';
}


namespace App\Models;

use Laravel\PricingPlans\Models\Plan as Model;

class Plan extends Model
{
    const PLAN_FREE = 'free';
    const PLAN_PRO = 'pro';
}



use Laravel\PricingPlans\Models\Feature;
use Laravel\PricingPlans\Models\Plan;

$feature1 = Feature::create([
    'name' => 'Upload images',
    'code' => 'upload-images',
    'description' => null,
    'interval_unit' => 'day',
    'interval_count' => 1,
    'sort_order' => 1,
]);

$feature2 = Feature::create([
    'name' => 'upload video',
    'code' => 'upload-video',
    'description' => null,
    'interval_unit' => 'day',
    'interval_count' => 1,
    'sort_order' => 2,
]);

$plan = Plan::create([
    'name' => 'Pro',
    'code' => 'pro',
    'description' => 'Pro plan',
    'price' => 9.99,
    'interval_unit' => 'month',
    'interval_count' => 1,
    'trial_period_days' => 5,
    'sort_order' => 1,
]);

$plan->features()->attach([
    $feature1->id => ['value' => 5, 'note' => 'Can upload maximum 5 images daily'],
    $feature2->id => ['value' => 1, 'note' => 'Can upload maximum 1 video daily'],
]);



use Illuminate\Support\Facades\Auth;
use Laravel\PricingPlans\Models\Plan;

$user = Auth::user();
$plan = Plan::code(Plan::PLAN_PRO)->firstOrFail();

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

$user->subscription('main')->ability()->canUse(Feature::FEATURE_UPLOAD_IMAGES);

$user->subscriptionUsage('main')->record(Feature::FEATURE_UPLOAD_IMAGES);

// Increment by 2
$user->subscriptionUsage('main')->record(Feature::FEATURE_UPLOAD_IMAGES, 2);

// Override with 9
$user->subscriptionUsage('main')->record(Feature::FEATURE_UPLOAD_IMAGES, 9, false);

$user->subscriptionUsage('main')->reduce(Feature::FEATURE_UPLOAD_IMAGES, 2);

$user->subscriptionUsage('main')->clear();

$user->subscribed('main');
$user->subscribed('main', $planId); // Check if user is using a particular plan

$user->subscription('main')->isActive();
$user->subscription('main')->isCanceled();
$user->subscription('main')->isCanceledImmediately();
$user->subscription('main')->isEnded();
$user->subscription('main')->onTrial();

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

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

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



use Laravel\PricingPlans\Models\PlanSubscription;

// Get subscriptions by plan:
$subscriptions = PlanSubscription::byPlan($plan_id)->get();

// Get subscription by subscriber:
$subscription = PlanSubscription::bySubscriber($user)->first();

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

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

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

// Get subscriptions with ended period:
$subscriptions = PlanSubscription::findEndedPeriod()->get();
bash
$ php artisan vendor:publish --provider="Laravel\PricingPlans\PricingPlansServiceProvider"
bash
$ php artisan migrate