Build SaaS-like apps with Plans & Guadian

Alex Renoki
Jul 21, 2018 · 3 min read

Hey, i love to see the internet growing up in this fast-paced way of selling products and having to make customers happy is a pretty hard task.

SaaS apps are pretty much the core of some projects nowawdays. Today i’ll teach you how to use Laravel Plans & Guardian to create your own SaaS app and limit access to users based on their current plan.

Before starting, i will let you know that Laravel Cashier works almost the same as Plans, but you won’t be able to consume limited features using Cashier.


Both Laravel Plans & Guardian are Laravel packages written by myself. If you have any other permissions package already in usage, like Bouncer, you can use it instead of Guardian, but i will rely on Guardian since i find it easier to use since i have created it… However, the main principles are the same, so don’t worry.

First of all, take a look at the Laravel Plans repository. You will find a comprehensive documentation over how it works, how to implement it and how to use it. If you decided you want to try Guardian, you can find instructions in its repository.


Well, at the base of the SaaS apps, there are plans. Plans are steps that cost, but they can bring more features.

Creating plans and adding features to them can be done using Laravel relationships:

$plan = PlanModel::create([
'name' => 'My awesome plan',
'description' => 'One of the best plans out here.',
'price' => 9.99,
'currency' => 'EUR',
'duration' => 30, // in days
]);

As you can see, we have a permission, called vault.access and a limited feature (that can be count) called build.minutes . We will treat the first one as a permission, while the second one as a consumable feature.

But first… let’s subscribe our user to the plan:

$user->subscribeTo($plan, 30); // Subscribed to plan for 30 days.

Alternatively, you can subscribe your users until a certain date, instead passing a number of days:

$user->subscribeToUntil($plan, '2018-12-21 16:54:11');

Whenever your user is subscribed, either it’s using subscribeTo() or subscribeToUntil, you can listen for that event using Laravel Events. For that, there are two events on you can listen:

Rennokki\Plans\Events\NewSubscription
Rennokki\Plans\Events\NewSubscriptionUntil

For documentation about data parse, check out the Events section from Plans documentation.


Since Laravel Cashier implements Stripe & Braintree, Laravel Plans comes only with Stripe integrated which can be used to charge subscribers. To do so, check the in-depth documentation on Payments on the repository’s readme and for recurrent payments, make sure you visit the Recurrency handling section.

If you have your own payment system, you can use Plans without the integrated Stripe Charge feature.


Now, that our user has subscribed, we can start allowing features (not limited features that are coutable) to our user through the permissions system:

foreach($event->subscription->features()->feature()->get() as $feature) {
$event->model->allow($feature->code);
}

You can later check permissions using can() method. Full reference can be found on Guardian’s GitHub repository.


If you give your users a limited amount of resources, Laravel Plans will help you track them.

$subscription = $user->activeSubscription();
$subscription->consumeFeature('build.minutes', 10);

Now, our user will have 10 less minutes allocated for the montly build minutes. You can also undo it:

$subscription->unconsumeFeature('build.minutes', 10);

Each method has a specific event going on, so check the Events section from Plans documentation to see all the events your app can listen for.


In the end, i consider that Laravel Plans is an easy solution to keep tracking of your SaaS apps plans, features & consumptions. Sooner, it will implement payment system for ease and automate process on payments.

Alex Renoki

Written by

Certified Laravel Developer. Working with full-stack web applications, AWS, Kubernetes and buzzwords like cloud computing.