LARAVEL

Introducing Laravel Spark: Alpha Release


This is an alpha, experimental release of Spark. Things will change. Things will break. Thank you for testing

Yes, the much talked about Laravel Spark has finally been released. It’s the Alpha release with a better and solidified API. I’ll quickly give you an overview of Laravel Spark and how to use it for quick business SAAS application. I mean, that’s the main reason it was created.

Laravel Spark handles user authentications, user roles, plans and payments, billing system, payments coupons and team management.

Installation

Run this command to install the spark installer globally:

Once it’s installed. cd to the directory you actually want your spark app to reside. Then run this command to create a new laravel app.

Note: You must have the laravel installer globally to run this command below: If you don’t, run this command to install the laravel installer:

Make sure to place the ~/.composer/vendor/bin directory in your PATH so the laravelexecutable can be located by your system.

cd into the business-app, then run:

There will be several prompts. Answer accordingly, but I chose yes for everything.

According to the Installation, you are required to go to your .env file and assign values to the AUTHY_KEY, STRIPE_KEY and STRIPE_SECRET environment variables.

Homestead Users

During the installation process, it ran some migrations but for the homestead users, we’ll have to ssh into our box and do that ourselves.

Open up your Homestead.yaml file, add a new database, set the path to your business-app folder

Do the required assignment of ip address in your hosts file.

Run your migrations inside homestead.

By the time you are done, you would have something like this:

Invitations, teams and user_teams tables have been added. In a default laravel installation, those 3 tables don’t exist.

So many files have been changed in this Spark Installation. Let’s look at one serious file that has been added.

The SparkServiceProvider.php file

SparkServiceProvider

As we know, provider files reside within the app/Providers directory.

Let’s go through each major section:

$invoiceWith Array

It’s self-explanatory, just substitute your real details in the values of the array keys.

customizeSpark

The method registers the team model. You can change it to whatever model you feel comfortable using.

customizeRegistration

This defines your registration logic . You can decide to have a custom Request file where you define all your validation rules and just inject it into the validateRegistrationsWith method and createUsersWith. That’ll work fine and make the method concise and clean

customizeRoles

This method integrates with the new ACL feature added in Laravel 5.1.11. You can add other custom roles in the Spark roles method.

customizeSettingsTabs

This function allows you to add or remove links in the settings tab.

Add a new link by uncommenting the

It will create a new link called Name on the settings tab.

Same goes for the teamSettings tab.

customizeProfileUpdates

This is similar to customizeRegistration. You can change the rules here to fit your style. I personally prefer creating a custom ProfileUpdate Request file where I define my validation rules and then inject them here into validateProfileUpdatesWith and updateProfilesWith methods

customizeSubscriptionPlans

This is where you adjust your subscription plans to meet your business needs. It’s so awesome that the boilerplate has been created for you easily. Let me show you  a typical example

Add this indicates on the UI that I have a Basic and Advanced plan. The Advanced plan has a free trial for 7 days. The Basic and Advanced plan is yearly with a subscription of 20 and 50 dollars respectively.

Just too awesome to have all that generated for you with just simple and minor adjustments to your code. Damn, too awesome!!!

Coupons

Laravel Spark offers coupons and discounts.  Create a coupon in Stripe, then send the users coupon as a parameter when they visit the registration page like so

Laravel Spark sees the coupon in the GET request, then automatically validates it from the Stripe API and applies the discount amount to the total like so:

You can also temporarily add a site-wide coupon by adding this in the boot() or customizeSpark() method in the SparkServiceProvider.php file.

Terms and Conditions

Create a new file named terms.md in the root directory and it will automatically be pulled in for the /terms route.

Models

We have the User and Team Models that ships with the installation.

User.php

The Model implements TwoFactorAuthenticableContract. We have the $hidden array populated with more fields that needs to be hidden from a json response.

Team.php

This is the boilerplate for the Team Model. You can add several methods to get the users that belong to a team, the owner of the team, invitations to a team and several other functionalities you might think of.

Laravel Spark also makes good use of  Vue.js and JQuery. Check the resources/assets/js directory. Vue.js components are loaded for the views. That’s what allows for the slick dynamic switching in the UI.

I don’t know why Taylor is obsessed with Vue.js. I think it’s because Vue.js is lightweight and very simple to use.

PS: This is just an introduction to the Alpha release of Laravel Spark. There might be other things that I didn’t cover, but I leave that as your assignment, dig it up to see the inner workings and let me know about your findings! :smile:

Please if you have any questions or observations, Feel free to drop it in the comments section below.

PROSPER OTEMUYIWA

About PROSPER OTEMUYIWA

Food Ninja, Code Slinger, Technical Trainer, Accidental Writer, Open Source Advocate and Developer Evangelist.