How to build a free plan in Laravel Spark

Search for a command to run...

No comments yet. Be the first to comment.
Introduction I run a large WhatsApp group and I often need to mention everyone in the group. But there are no real solutions for mentioning all members. WhatsApp does not natively do it, and there was a Firefox browser extension, but it was abandoned...

Introduction This past week I was toying around with the HTML5 canvas element, trying to join two images together. At first, it seemed fine, but when I tried to reload the website, it was a mess. One image would load, but the other wouldn't. Investig...

Introduction I have worked on projects that required the production and testing database to be the same. This could be because some features work in MariaDB, but not in SQLite. Or some bugs appear in MySQL, but not in PostgreSQL. When you're working ...

Introduction Sometimes you end up deploying an application to Dokku and then realize that you want to revert the changes you made. In this tutorial we'll go over how to roll back a Dokku deployment. Before we start Preface Keep in mind that rolling ...

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.
You should know that Laravel Spark is built on top of Laravel Cashier, so know that the process is very similar with both.
Before starting the tutorial, you should have followed the official installation guide.
Let's imagine that we are building a new application which offers a free plan and a paid plan.
We need to configure both plans in the config/spark.php file, like so:
// ...
'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.
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.
// ...
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.
Through your application you can access a user plan and limits like so:
$user = User::find(1);
$plan = $user->getPlan();
$plan->name; // "Free"
$plan->options['projects']; // 1
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.
If you have found this useful, then you should follow me, I will be posting more interesting content! 🥰
Or support me financially. 💸
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!