Laravel Spark

Please Note: This article was written when the Alpha version of Spark first released. It is no longer accurate as Spark is now a commercial project. Have a look at this Laravel Spark article for the new version.

The Alpha of Laravel Spark has just been released and its goal is to be an opinionated way of building out business oriented SaaS applications. It features team management, user roles, recurring billing through Stripe, and much more. In this tutorial let’s take a deeper look at this new package.

Spark is designed with only one goal in mind, to make scaffolding out a billing system for a SaaS app easy. If you’ve ever built out team management and a billing system then you already know how time-consuming and painful this process is. There is so much tedious work hooking up all the different systems, designing it, creating invoices, and on and on. By using Spark, you can put your focus on what matters, the business.

So let’s jump in and take a first look at Laravel Spark.

Please note that Spark is an ALPHA release. Things will change and things will break. This post is meant to give you an overview of what it features right now. As Spark stabilizes I will update this post as appropriate.

Spark Installation

The installation of Spark is straightforward by utilizing a global composer package that does all the heavy lifting. To install this open your terminal and type in:

composer global require "laravel/spark-installer=~1.0"

Next, either create a new Laravel app or go inside your existing and run:

spark install

Once this runs it will ask you a few questions from the console:

Spark Install

As you can see it tells you everything you need and will run all the commands for you. Of course, you can always run these commands manually with:

php artisan migrate

npm install

gulp

At the end of the install, it warns you to setup your Stripe tokens and an Authy key in your .env file. Authy is a two-factor authentication (2FA) system for protecting logins and you can signup for a free API key.

Now with the basic install completed let’s dive into setting it up.

SparkServiceProvider

The SparkServiceProvider handles a lot of the settings for Spark and is really powerful. Open the file and let’s go through each major section covering the basic details on what it does.

This will be one of the first things you change and should be pretty self-explanatory.

customizeSpark Method

By default all this method does is register the team model:

Spark::configure([
'models' => [
'teams' => Team::class,
]
]);

You can add other configuration, change the team model, and more.

Customize Registration and Profile Updates

The customizeRegistration and customizeProfileUpdates methods are designed to add new validation rules to these forms and/or to adjust the way users are saved. This allows you to do any custom processing.

Customize Roles

The customizeRoles integrates with Laravel 5.1.11’s new authorization feature. You can set a default role:

Spark::defaultRole('member');

Plus add your own roles:

Spark::roles([
'admin' => 'Administrator',
'member' => 'Member',
]);

Customize Settings Tabs

The customizeSettingsTabs allows you to add or remove new links in the settings forms. For example, the member settings looks like this:

Spark Default Settings

Now, if you want to add a new link you can do so by “making” a new tab under the current list:

The order of parameters of $tabs->make is title, view file, icon. So in my example, I named the link “My New Setting”, created a new blade file at resources/views/test.blade.php, and used the fa-rocket icon from FontAwesome.

New Link Added to Spark Settings

You can also do the same for the tabs on the team settings page as well. It follows the same format.

Building Spark Subscription Payment Plans

Locate the customizeSubscriptionPlans method and adjust the plans to match your business needs. In this example, I created two plans with 10-day trials, that can also be purchased yearly:

Spark really shines with the visual representation of these plans. When I added a yearly plan. The UI changed with a button to swap between monthly and yearly:

Spark Plans

Before a purchase can be made you will need to edit the ‘stripe-id’ and insert a real one.

Coupons and Discounts

No order system would be complete without the ability to offer customers coupons and discounts. Spark enables this in two ways.

The first is by coupons. Inside Stripe create a new one and then send your user to the register page like this:

register?coupon=10_percent

Once Spark sees the coupon in the GET request it will automatically validate it from the Stripe API and apply the discount amount to the total.

Spark Coupon Applied

Site wide discounts work much the same way. The only difference is you define it in the Spark Service Provider. Inside boot() or customizeSpark() add the following:

Spark::promotion('10_percent');

Now that discount will be applied to any order.

Terms & Conditions

Create a new file named terms.md in the root of your install and it will automatically be pulled in for the /terms route. The markdown parsing is handled by Parsedown.

Closing

Spark is opinionated but very powerful. The next time you need to setup recurring billing I would recommend giving Spark a try. It will save you tons of time and effort.

Please note that Spark is an ALPHA release. Things will change and things will break. This post is meant to give you an overview of what it features right now. As Spark stabilizes I will update this post as appropriate.