PHP code example of hoceineel / laravel-modular-subscriptions

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

    

hoceineel / laravel-modular-subscriptions example snippets


return [
    'modules' => [
        // Add your default modules here
    ],
    'models' => [
        'plan' => HoceineEl\LaravelModularSubscriptions\Models\Plan::class,
        'subscription' => HoceineEl\LaravelModularSubscriptions\Models\Subscription::class,
        'module' => HoceineEl\LaravelModularSubscriptions\Models\Module::class,
        'usage' => HoceineEl\LaravelModularSubscriptions\Models\ModuleUsage::class,
    ],
];

use HoceineEl\LaravelModularSubscriptions\Modules\BaseModule;
use HoceineEl\LaravelModularSubscriptions\Models\Subscription;

class SubscribersModule extends BaseModule
{
    public function getName(): string
    {
        return 'subscribers';
    }

    public function getLabelKey(): string
    {
        return 'Subscribers'; // we will use it like __('Subscribers')
    }

    public function calculateUsage(Subscription $subscription): int
    {
        return $subscription->subscribable->subscribers()->count();
    }

    public function getPricing(Subscription $subscription): float
    {
        $subscriberCount = $this->calculateUsage($subscription);
        return $subscriberCount * 0.5; // $0.50 per subscriber
    }

    public function canUse(Subscription $subscription): bool
    {
        $usage = $this->calculateUsage($subscription);
        $limit = 1000; // Example limit
        return $usage < $limit;
    }
}

use HoceineEl\LaravelModularSubscriptions\Traits\Subscribable;

class User extends Model
{
    use Subscribable;
    // ...
}

$user->newSubscription($planId, $trialDays);

if ($user->hasSubscription()) {
    // User has an active subscription
}

if ($user->onTrial()) {
    // User is on trial
}

$daysLeft = $user->daysLeft();

$user->cancel();

$user->renew(30); // Renew for 30 days

$user->changePlan($newPlanId);

$user->recordUsage('subscribers', 5);

if ($user->canUseModule('subscribers')) {
    // User can use the subscribers module
}

$user->extendTrial(7); // Extend trial by 7 days

$user->endTrial();

use HoceineEl\LaravelModularSubscriptions\Facades\ModularSubscriptions;

$subscription = $user->activeSubscription();
$totalUsage = ModularSubscriptions::totalUsage($subscription);
$totalPricing = ModularSubscriptions::totalPricing($subscription);
bash
php artisan vendor:publish --provider="HoceineEl\LaravelModularSubscriptions\ModularSubscriptionsServiceProvider" --tag="config"
bash
php artisan vendor:publish --provider="HoceineEl\LaravelModularSubscriptions\ModularSubscriptionsServiceProvider" --tag="migrations"
bash
php artisan migrate