PHPackages                             raphyabak/laravel-subscriptions - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Admin Panels](/categories/admin)
4. /
5. raphyabak/laravel-subscriptions

ActiveLibrary[Admin Panels](/categories/admin)

raphyabak/laravel-subscriptions
===============================

Complete subscription management system for Laravel

1.0.0(1y ago)018MITPHPPHP ^8.0

Since Mar 8Pushed 1y ago1 watchersCompare

[ Source](https://github.com/raphyabak/laravel-subscriptions)[ Packagist](https://packagist.org/packages/raphyabak/laravel-subscriptions)[ RSS](/packages/raphyabak-laravel-subscriptions/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

Laravel Subscriptions Package Documentation
===========================================

[](#laravel-subscriptions-package-documentation)

Introduction
------------

[](#introduction)

The **Laravel Subscriptions** package offers a straightforward and flexible solution for managing subscriptions in your Laravel application. With this package, you can:

- Create subscription plans.
- Manage user subscriptions.
- Control access to application features based on subscription status.

Installation
------------

[](#installation)

To get started, install the package via Composer:

```
composer require raphyabak/laravel-subscriptions
```

Configuration
-------------

[](#configuration)

After installation, publish the configuration file to customize the package settings:

```
php artisan vendor:publish --provider="Raphyabak\Subscription\Providers\SubscriptionServiceProvider" --tag="subscription-config"
```

This command generates a `config/subscription.php` file. You can modify the package settings in this file to suit your application.

### Configuration Options

[](#configuration-options)

Here are the key options you can configure in `config/subscription.php`:

1. **User Model**:

    - Specify the fully qualified class name of your User model.
    - Default: `App\Models\User`
2. **Models**:

    - Override the default models if needed:
        - `plan`: Model for subscription plans. Default: `Raphyabak\Subscription\Models\Plan`
        - `subscription`: Model for user subscriptions. Default: `Raphyabak\Subscription\Models\Subscription`
3. **Table Names**:

    - Customize the table names used by the package:
        - `plans`: Table for subscription plans. Default: `plans`
        - `subscriptions`: Table for user subscriptions. Default: `subscriptions`

#### Example Configuration:

[](#example-configuration)

```
return [
    'user_model' => App\Models\CustomUser::class,
    'models' => [
        'plan' => App\Models\CustomPlan::class,
        'subscription' => App\Models\CustomSubscription::class,
    ],
    'tables' => [
        'plans' => 'custom_plans',
        'subscriptions' => 'custom_subscriptions',
    ],
];
```

> **Note:** If you change the table names, remember to update your database migrations accordingly.

Database Migrations
-------------------

[](#database-migrations)

Run the included database migrations to create the necessary tables:

```
php artisan migrate
```

This command creates the following tables:

- `plans`: Stores subscription plans.
- `subscriptions`: Stores user subscriptions.

If you customized the table names in the configuration, the migrations will reflect those changes.

Usage
-----

[](#usage)

### Setting Up the User Model

[](#setting-up-the-user-model)

To enable subscription features, add the `HasSubscriptions` trait to your User model.

1. Import and apply the trait:

```
use Raphyabak\Subscription\Traits\HasSubscriptions;

class User extends Authenticatable
{
    use HasSubscriptions;

    // Additional user model code...
}
```

2. If your User model is customized or resides in a different namespace, update the `user_model` option in `config/subscription.php`:

```
return [
    'user_model' => App\Models\CustomUser::class,
];
```

The `HasSubscriptions` trait provides useful methods, such as:

- `subscriptions()`: Retrieve all subscriptions for the user.
- `activeSubscription()`: Retrieve the user's active subscription.
- `isSubscribed()`: Check if the user has an active subscription.
- `subscribeTo(Plan $plan, $duration = null)`: Subscribe the user to a plan.
- `cancelSubscription()`: Cancel the user's active subscription.
- `hasFeature($feature)`: Check if the user's active subscription includes a specific feature.

### Creating Subscription Plans

[](#creating-subscription-plans)

You can create subscription plans programmatically or through your admin interface. Here's an example:

```
use Raphyabak\Subscription\Models\Plan;

$plan = Plan::create([
    'name' => 'Pro Plan',
    'description' => 'Access to all features',
    'price' => 29.99,
    'duration' => 30, // Duration in days
    'trial_days' => 7, // Trial period in days
    'features' => ['feature1', 'feature2', 'feature3'],
]);
```

### Managing User Subscriptions

[](#managing-user-subscriptions)

#### Subscribe a User to a Plan

[](#subscribe-a-user-to-a-plan)

```
$user = User::find(1);
$plan = Plan::find(1);

// Subscribe the user for the plan's default duration
$subscription = $user->subscribeTo($plan);

// Subscribe the user for a custom duration (e.g., 7 days)
$subscription = $user->subscribeTo($plan, 7);

// Subscribe the user for a specific period (e.g., monthly, yearly)
$subscription = $user->subscribeTo($plan, 30); // Monthly
$subscription = $user->subscribeTo($plan, 365); // Yearly
```

#### Check If a User Is Subscribed

[](#check-if-a-user-is-subscribed)

```
if ($user->isSubscribed()) {
    // The user has an active subscription
}
```

#### Retrieve Active Subscription

[](#retrieve-active-subscription)

```
$activeSubscription = $user->activeSubscription();
```

#### Cancel a Subscription

[](#cancel-a-subscription)

```
$user->cancelSubscription();
```

### Access Control with Gates

[](#access-control-with-gates)

The package provides gates for managing access to features and plans. These gates are registered automatically.

#### Available Gates

[](#available-gates)

1. `has-feature`: Check if the user has a specific feature.
2. `subscribed-to-plan`: Check if the user is subscribed to a specific plan.
3. `is-subscribed`: Check if the user has any active subscription.

#### Using Gates in Controllers

[](#using-gates-in-controllers)

```
if (Gate::allows('has-feature', 'premium_content')) {
    // User has access to premium content
}

if (Gate::allows('subscribed-to-plan', 'pro')) {
    // User is subscribed to the 'Pro' plan
}

if (Gate::allows('is-subscribed')) {
    // User has an active subscription
}
```

#### Using Gates in Blade Templates

[](#using-gates-in-blade-templates)

```
@can('has-feature', 'premium_content')
    Access to premium content
@endcan

@can('subscribed-to-plan', 'pro')
    Welcome to the Pro Plan!
@endcan

@can('is-subscribed')
    Thank you for subscribing!
@else
    Please subscribe to access more features.
@endcan
```

### Middleware for Access Control

[](#middleware-for-access-control)

The package includes middleware for route-level access control:

1. `subscribed`: Ensures the user has an active subscription.
2. `feature`: Ensures the user's subscription includes a specific feature.
3. `throttle_by_subscription`: Throttles requests based on the user's subscription.

#### Example Usage

[](#example-usage)

```
// Require any active subscription
Route::middleware(['subscribed'])->group(function () {
    // Routes for subscribed users
});

// Require subscription to a specific plan
Route::middleware(['subscribed:pro'])->group(function () {
    // Routes for 'Pro' plan users
});

// Require access to a specific feature
Route::middleware(['feature:premium_feature'])->group(function () {
    // Routes for users with the 'premium_feature'
});
```

### Scheduled Subscription Status Checks

[](#scheduled-subscription-status-checks)

The package includes a `CheckSubscriptionStatus` Artisan command to automatically update subscription statuses:

```
php artisan subscription:check-status
```

To automate this, schedule the command in `App\Console\Kernel`:

```
protected function schedule(Schedule $schedule)
{
    $schedule->command('subscription:check-status')->daily();
}
```

Events
------

[](#events)

The package emits the following events:

- `SubscriptionCreated`: Triggered when a new subscription is created.
- `SubscriptionCancelled`: Triggered when a subscription is canceled.

You can listen to these events to execute custom actions.

Contributing
------------

[](#contributing)

Contributions are welcome! Feel free to submit a pull request to improve the package.

License
-------

[](#license)

This package is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance45

Moderate activity, may be stable

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

431d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/dc81afa7da07e35e6df38b54ea51ef3bac1a526941ba048e86bd92d70fa252b3?d=identicon)[raphyabak](/maintainers/raphyabak)

---

Top Contributors

[![raphyabak](https://avatars.githubusercontent.com/u/58911706?v=4)](https://github.com/raphyabak "raphyabak (12 commits)")

### Embed Badge

![Health badge](/badges/raphyabak-laravel-subscriptions/health.svg)

```
[![Health](https://phpackages.com/badges/raphyabak-laravel-subscriptions/health.svg)](https://phpackages.com/packages/raphyabak-laravel-subscriptions)
```

###  Alternatives

[backpack/crud

Quickly build admin interfaces using Laravel, Bootstrap and JavaScript.

3.4k3.4M207](/packages/backpack-crud)[conedevelopment/root

Root is an admin package for Laravel applications.

3713.1k2](/packages/conedevelopment-root)[eveseat/web

SeAT Web Interface

2723.2k135](/packages/eveseat-web)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
