How to build a free plan in Laravel Spark
Tags: tutorial, laravel, stripe, paddle
Published: 2021-10-10 11:20:23

## Introduction

I'm going to explain how to build a free plan for your Laravel Spark Next application.

I will be using Paddle as the payment gateway, but the steps are almost identical with Stripe.

Best of all? No credit card required for the free plan.

---

## Before we start

### Preface

You should know that Laravel Spark is built on top of Laravel Cashier, so know that the process is very similar with both.

### Requirements

- Laravel 8 with Spark Next

---

## Laravel Spark installation

Before starting the tutorial, you should have followed the [official installation guide](https://spark.laravel.com/docs/1.x/installation.html).

---

## Laravel Spark plan configuration

Let's imagine that we are building a new application which offers a free plan and a paid plan.

- The **"Free" plan** will have a limit of **1 project**.
- The **"Paid" plan** will have a limit of **5 projects**.

We need to configure both plans in the `config/spark.php` file, like so:

```php

// ...

  'plans' => [
      [
          'name' => 'Free',
          'short_description' => 'This is the free plan',
          // Random _(not used by other plans)_ ID
          'monthly_id' => 1000,
          'features' => [
              '1 Project',
          ],
          'options' => [
              'projects' => 1,
          ],
          // IMPORTANT
          'archived' => true,
      ],
      [
          'name' => 'Paid',
          'short_description' => 'This is the paid plan',
          'monthly_id' => 999990,
          'yearly_id' => 999991,
          'yearly_incentive' => 'Save 20%!',
          'features' => [
              '5 Projects',
          ],
          'options' => [
              'projects' => 5,
          ],
          'archived' => false,
      ],
  ]

// ...

```

As you can see, the limits are actually set in the `options` array.

It's important that the "Free" plan is set to `archived` to prevent users from selecting that plan on the billing page.

The "Free" plan has to have a random `monthly_id`, so the plan can be used internally.
_It doesn't need to be a working product ID, nor do you have to create it on Paddle/Stripe_.

---

## Modify User model

Now that we have created a "Free" plan, we need to modify the `User` model to add a `getPlan` method.

This method will get the current users plan, **or the "Free" plan if the user has no active plan**.

```php

// ...

    public function getPlan()
    {
        $plan = $this->sparkPlan();

        if ($plan !== null) {
            return $plan;
        }

        // Fallback to "Free" plan
        $plan = Spark::plans('user')->firstWhere('name', '=', 'Free');

        return $plan;
    }

// ...

```

Note: As mentioned in the steps before, you need to add a `monthly_id` to the "Free" plan, otherwise it will be skipped by the `Spark::plans('user')` call and will not be found.
The ID can be random, don't worry.

---

## Getting the User's plan

Through your application you can access a user plan and limits like so:

```php
$user = User::find(1);

$plan = $user->getPlan();

$plan->name; // "Free"

$plan->options['projects']; // 1
```

---

## End

As you can see, what we actually do is fallback to the "Free" plan if the user has no active plan.

And this plan doesn't actually go through the payment process, so you don't need to ask the users with a credit card.

<!-- INCLUDE -->

### Self-promotion

If you have found this useful, then you should follow me, I will be posting more interesting content! 🥰

- [GitHub](https://redirect.akbal.dev/github)
- [Twitter](https://redirect.akbal.dev/twitter)
- [Dev.to](https://redirect.akbal.dev/dev.to)

Or support me financially. 💸

- [GitHub Sponsors](https://redirect.akbal.dev/github/sponsor)
- [LiberaPay](https://redirect.akbal.dev/liberapay)
- [PayPal](https://redirect.akbal.dev/paypal)

<!-- /INCLUDE -->

### Conclusion

Congratulations, today you have learned how to build a free plan in Laravel Spark.

**Let me know if the tutorial was useful to you in the comments!**