1. Go to this page and download the library: Download turahe/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/ */
turahe / subscription example snippets
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Turahe\Subscription\Traits\HasPlanSubscriptions;
class User extends Authenticatable
{
use HasPlanSubscriptions;
// ... your existing code
}
use Turahe\Subscription\Models\Plan;
use Turahe\Subscription\Models\PlanFeature;
use Turahe\Subscription\Enums\Interval;
// Create a plan
$plan = Plan::create([
'name' => 'Pro Plan',
'description' => 'Professional plan with advanced features',
'price' => 29.99,
'signup_fee' => 0,
'invoice_period' => 1,
'invoice_interval' => Interval::MONTH,
'trial_period' => 14,
'trial_interval' => Interval::DAY,
'currency' => 'USD',
'is_active' => true,
]);
// Add features to the plan
$plan->features()->saveMany([
new PlanFeature([
'name' => 'api_calls',
'value' => 1000,
'sort_order' => 1,
'resettable_period' => 1,
'resettable_interval' => 'month'
]),
new PlanFeature([
'name' => 'storage_gb',
'value' => 10,
'sort_order' => 2
]),
new PlanFeature([
'name' => 'priority_support',
'value' => 'Y',
'sort_order' => 3
])
]);
$user = User::find(1);
$plan = Plan::find(1);
// Create a new subscription
$subscription = $user->newPlanSubscription('main', $plan);
// Check if user is subscribed to a plan
$user->subscribedTo($plan->id);
// Check if subscription is active
$user->planSubscription('main')->active();
// Check if on trial
$user->planSubscription('main')->onTrial();
// Check if user can use a feature
$canUse = $user->planSubscription('main')->canUseFeature('api_calls');
// Get remaining uses
$remaining = $user->planSubscription('main')->getFeatureRemainings('api_calls');
// Get current usage
$usage = $user->planSubscription('main')->getFeatureUsage('api_calls');
// Record single usage
$user->planSubscription('main')->recordFeatureUsage('api_calls');
// Record multiple uses
$user->planSubscription('main')->recordFeatureUsage('api_calls', 5);
// Override usage (non-incremental)
$user->planSubscription('main')->recordFeatureUsage('api_calls', 10, false);
// Reduce usage by 1
$user->planSubscription('main')->reduceFeatureUsage('api_calls');
// Reduce usage by specific amount
$user->planSubscription('main')->reduceFeatureUsage('api_calls', 3);
// Get all active subscriptions for a user
$activeSubscriptions = $user->activePlanSubscriptions();
// Get subscribed plans
$subscribedPlans = $user->subscribedPlans();
use Turahe\Subscription\Models\PlanSubscription;
// Get subscriptions by plan
$subscriptions = PlanSubscription::byPlanId($planId)->get();
// Get user's subscriptions
$userSubscriptions = PlanSubscription::ofSubscriber($user)->get();
// Get subscriptions ending trial in 3 days
$endingTrials = PlanSubscription::findEndingTrial(3)->get();
// Get subscriptions ending period in 7 days
$endingPeriods = PlanSubscription::findEndingPeriod(7)->get();