Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for How to build a free plan in Laravel Spark
Alejandro Akbal
Alejandro Akbal

Posted on • Originally published atblog.akbal.dev

     

How to build a free plan in Laravel Spark

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 theofficial installation guide.


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 of1 project.
  • The"Paid" plan will have a limit of5 projects.

We need to configure both plans in theconfig/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,],]// ...
Enter fullscreen modeExit fullscreen mode

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

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

The "Free" plan has to have a randommonthly_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 theUser model to add agetPlan method.

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

// ...publicfunctiongetPlan(){$plan=$this->sparkPlan();if($plan!==null){return$plan;}// Fallback to "Free" plan$plan=Spark::plans('user')->firstWhere('name','=','Free');return$plan;}// ...
Enter fullscreen modeExit fullscreen mode

Note: As mentioned in the steps before, you need to add amonthly_id to the "Free" plan, otherwise it will be skipped by theSpark::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:

$user=User::find(1);$plan=$user->getPlan();$plan->name;// "Free"$plan->options['projects'];// 1
Enter fullscreen modeExit fullscreen mode

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.

Self-promotion

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

Or support me financially. 💸

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!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Developer
  • Location
    Spain
  • Joined

More fromAlejandro Akbal

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp