PHP code example of hrdrv / laravel-onboard

1. Go to this page and download the library: Download hrdrv/laravel-onboard library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hrdrv / laravel-onboard example snippets


class User extends Model
{
    use \WF\Onboard\GetsOnboarded;
    // ...
}

    public function boot()
    {
        // This step will only apply to User::class:
        OnboardFacade::addStep('Create your first post', User::class)
            ->link('/post/create')
            ->cta('Create Post')
            // ->cacheResults() // You can cache the results to avoid duplicating queries
            ->completeIf(function (User $user) {
                // This will make 1 DB query each time to retrieve the count
                // The result will be cached if using cacheResults()
                return $user->posts()->count() > 0;
            })
            // You may add a scope to only fetch users having completed this step
            // This scope will be used when querying User::onboarded()->get()
            ->completeScope(function (Builder $builder) {
                $builder->whereHas('posts');
            })
            // All steps are 

@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="{{ $step->link }}" {{ $step->complete() ? 'disabled' : '' }}>
                {{ $step->cta }}
            </a>
        @endforeach

    </div>
@endif

User::onboarded()->get()->each->notify(new OnboardingComplete);
// User::onboarded(true by default)
User::onboarded(false)->get()->each->notify(new OnboardingIncomplete);

$onboarding = Auth::user()->onboarding();

$onboarding->inProgress();
$onboarding->finished();
$onboarding->finishedRequired();
$onboarding->nextUnfinishedStep();
$onboarding->step($code); // returns the step you're looking for

$onboarding->steps()->each(function($step) {
    $step->code;
    $step->title;
    $step->cta;
    $step->link;
    $step->complete();
    $step->incomplete();
    $step->

// Defining the attributes
// Closures will be resolved using the given onboarding user as their only argument 
OnboardFacade::addStep('Step w/ custom attributes', User::class)
    ->setAttributes([
        'name' => 'Waldo',
        'shirt_color' => 'Red & White',
        'shirt_price' => function (User $user) {
            return $user->age * 4; // yes, that example sucks :)
        },
    ]);

// Accessing them
$step->name;
$step->shirt_color;
$step->shirt_price;



namespace App\Http\Middleware;

use Illuminate\Support\Facades\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);
    }
}