Laravel Spark

Published on by

Laravel Spark image

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:

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.

invoiceWith

The invoiceWith is used when generating email invoices for customers:

protected $invoiceWith = [
'vendor' => 'Your Company',
'product' => 'Your Product',
'street' => 'PO Box 111',
'location' => 'Your Town, 12345',
'phone' => '555-555-5555',
];

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](https://i0.wp.com/d1zj60nuin5mrx.cloudfront.net/media/2015/09/16073412/spark-settings.png?resize=525%2C398&ssl=1)
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:
Spark::settingsTabs()->configure(function ($tabs) {
return [
$tabs->profile(),
$tabs->teams(),
$tabs->security(),
$tabs->subscription(),
$tabs->make('My New Setting', 'test', 'fa-rocket'),
];
});

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](https://i2.wp.com/d1zj60nuin5mrx.cloudfront.net/media/2015/09/16073540/spark-new-settings-link.png?resize=525%2C458&ssl=1)
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::plan('Basic', 'stripe-id')->price(10)
->features([
'Feature 1',
]);
Spark::plan('Advanced', 'stripe-id')->price(40)
->trialDays(7)
->features([
'Feature 1',
'Feature 2',
]);
 
Spark::plan('Basic', 'stripe-id')->price(10)
->yearly()
->features([
'Feature 1',
]);
Spark::plan('Advanced', 'stripe-id')->price(40)
->yearly()
->features([
'Feature 1',
'Feature 2',
]);

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](https://i1.wp.com/d1zj60nuin5mrx.cloudfront.net/media/2015/09/16074445/spark-plan.png?resize=525%2C328&ssl=1)
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](https://i0.wp.com/d1zj60nuin5mrx.cloudfront.net/media/2015/09/16075021/spark-coupon.png?resize=525%2C279&ssl=1)
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.

Eric L. Barnes photo

Eric is the creator of Laravel News and has been covering Laravel since 2012.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB

The latest

View all →
The Inertia.js v2 Beta is Here image

The Inertia.js v2 Beta is Here

Read article
Mastering Laravel, No Compromises, and SourceDive with Joel Clermont image

Mastering Laravel, No Compromises, and SourceDive with Joel Clermont

Read article
Now you can install PHP and the Laravel installer with a single command image

Now you can install PHP and the Laravel installer with a single command

Read article
WireSpy is a Sleek New Debug Bar for Laravel Livewire image

WireSpy is a Sleek New Debug Bar for Laravel Livewire

Read article
The Laracon AU schedule is now live image

The Laracon AU schedule is now live

Read article
Supercharge PhpStorm with Laravel Idea image

Supercharge PhpStorm with Laravel Idea

Read article