Package

Spatie Laravel Onboard

A Laravel package to help track user onboarding steps

Package

spatie/laravel-onboard

View on GitHub →

A Laravel package to help track user onboarding steps

This package lets you set up an onboarding flow for your application's users.

Here's an example of how it's set up:

use App\User;
use Spatie\Onboard\Facades\Onboard;
 
Onboard::addStep('Complete Profile')
->link('/profile')
->cta('Complete')
->completeIf(function (User $model) {
return $model->profile->isComplete();
});
 
Onboard::addStep('Create Your First Post')
->link('/post/create')
->cta('Create Post')
->completeIf(function (User $model) {
return $model->posts->count() > 0;
});

You can then render this onboarding flow however you want in your templates:

@if (auth()->user()->onboarding()->inProgress())
<div>
@foreach (auth()->user()->onboarding()->steps as $step)
<span>
@if($step->complete())
<i class="fa fa-check-square-o fa-fw"></i>
<s>{{ $loop->iteration }}. {{ $step->title }}</s>
@else
<i class="fa fa-square-o fa-fw"></i>
{{ $loop->iteration }}. {{ $step->title }}
@endif
</span>
 
<a href="https://github.com/spatie/laravel-onboard/blob/main/{{ $step->link }}" {{ $step->complete() ? 'disabled' : '' }}>
{{ $step->cta }}
</a>
@endforeach
</div>
@endif

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/laravel-onboard

Usage

Add the Spatie\Onboard\Concerns\GetsOnboarded trait and Spatie\Onboard\Concerns\Onboardable interface to any model or class in your app, for example the User model:

class User extends Model implements \Spatie\Onboard\Concerns\Onboardable
{
use \Spatie\Onboard\Concerns\GetsOnboarded;
...

Example configuration

Configure your steps in your App\Providers\AppServiceProvider.php

use App\User;
use Spatie\Onboard\Facades\Onboard;
 
class AppServiceProvider extends ServiceProvider
{
// ...
 
public function boot()
{
Onboard::addStep('Complete Profile')
->link('/profile')
->cta('Complete')
/**
* The completeIf will pass the class that you've added the
* interface & trait to. You can use Laravel's dependency
* injection here to inject anything else as well.
*/
->completeIf(function (User $model) {
return $model->profile->isComplete();
});
 
Onboard::addStep('Create Your First Post')
->link('/post/create')
->cta('Create Post')
->completeIf(function (User $model) {
return $model->posts->count() > 0;
});

The variable name passed to the completeIf callback must be $model.

Usage

Now you can access these steps along with their state wherever you like. Here is an example blade template:

@if (auth()->user()->onboarding()->inProgress())
<div>
@foreach (auth()->user()->onboarding()->steps as $step)
<span>
@if($step->complete())
<i class="fa fa-check-square-o fa-fw"></i>
<s>{{ $loop->iteration }}. {{ $step->title }}</s>
@else
<i class="fa fa-square-o fa-fw"></i>
{{ $loop->iteration }}. {{ $step->title }}
@endif
</span>
 
<a href="https://github.com/spatie/laravel-onboard/blob/main/{{ $step->link }}" {{ $step->complete() ? 'disabled' : '' }}>
{{ $step->cta }}
</a>
@endforeach
</div>
@endif

Check out all the available features below:

/** @var \Spatie\Onboard\OnboardingManager $onboarding **/
$onboarding = Auth::user()->onboarding();
 
$onboarding->inProgress();
 
$onboarding->percentageCompleted();
 
$onboarding->finished();
 
$onboarding->steps()->each(function($step) {
$step->title;
$step->cta;
$step->link;
$step->complete();
$step->incomplete();
});

Excluding steps based on condition:

Onboard::addStep('Excluded Step')
->excludeIf(function (User $model) {
return $model->isAdmin();
});

Limiting steps to a specific class:

Onboard::addStep('Limited Step', User::class)
->link('/post/create');
 
// or
 
Onboard::addStep('Limited Step', 'App\Models\User')
->link('/post/create');

When using limited steps, steps that are not limited will be available to all classes. For example:

// Defining User steps
Onboard::addStep('Limited User Step', User::class)
->link('/post/create');
 
// Defining Team steps
Onboard::addStep('Limited Team Step', Team::class)
->link('/post/create');
 
// Defining a step that is available to all classes
Onboard::addStep('Normal Step')
->link('/post/create');

The above will result in 1 step being available to all classes, and 2 steps being available to the User and Team classes:

Other classes will only see the Normal Step. User classes will both see the Normal Step and Limited User Step. Team classes will both see the Normal Step and Limited Team Step.

Definining custom attributes and accessing them:

// Defining the attributes
Onboard::addStep('Step w/ custom attributes')
->attributes([
'name' => 'Waldo',
'shirt_color' => 'Red & White',
]);
 
// Accessing them
$step->name;
$step->shirt_color;

Example middleware

If you want to ensure that your User is redirected to the next unfinished onboarding step, whenever they access your web application, you can use the following middleware as a starting point:

<?php
 
namespace App\Http\Middleware;
 
use Auth;
use Closure;
 
class RedirectToUnfinishedOnboardingStep
{
public function handle($request, Closure $next)
{
if (auth()->user()->onboarding()->inProgress()) {
return redirect()->to(
auth()->user()->onboarding()->nextUnfinishedStep()->link
);
}
 
return $next($request);
}
}

Quick tip: Don't add this middleware to routes that update the state of the onboarding steps, your users will not be able to progress because they will be redirected back to the onboarding step.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

The original code from this package came from Onboard by Caleb Porzio, who was gratious enough to let us continue development.

License

The MIT License (MIT). Please see License File for more information.

Spatie Laravel Onboard Related Articles

A Package for Onboarding Users in Laravel Apps image

A Package for Onboarding Users in Laravel Apps

Read article
Pushsilver logo

Pushsilver

Pushsilver is simple invoicing for freelancers and small studios — send polished invoices, automate reminders, and get paid on time without the bloat.

Pushsilver
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 $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
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