Skip to content

Instantly share code, notes, and snippets.

@dillinghamio
Last active December 26, 2020 13:24
Show Gist options
  • Save dillinghamio/9b79dfab580d0f46971782ef9204e507 to your computer and use it in GitHub Desktop.
Save dillinghamio/9b79dfab580d0f46971782ef9204e507 to your computer and use it in GitHub Desktop.
Laravel Spark @plan Blade Directive

@plan Blade Directive For Laravel Spark

Works with user & team billing

Add this to the boot() method of your AppServiceProvider

\Blade::directive('plan', function($plans) {

        $model = auth()->user();

        if(\Spark::usesTeams())
        {
            $model = $model->currentTeam();
        }

        $activePlan = $model->sparkPlan()->id;

        return "<?php if((is_array(with{$plans})) ? in_array('$activePlan', with{$plans}) : with{$plans} == '$activePlan') : ?>";
});

\Blade::directive('endplan', function() {

        return "<?php endif; ?>";
});

And use like so

@plan('free')
    <h1>Free</h1>
@endplan

@plan('provider-id-1')
    <h1>Plan 1</h1>
@endplan

@plan('provider-id-2')
    <h1>Plan 2</h1>
@endplan

Can also pass an array

@plan(['provider-id-1', 'provider-id-2'])
    <h1>Plan 1 or Plan 2</h1>
@endplan

See also @role Blade Directive For Laravel Spark

@azimidev
Copy link

azimidev commented Sep 9, 2017

@dillinghamio
It does not work! What's with{$plan} is doing inside in_array?
Also what if user is not logged in??

Parse error: syntax error, unexpected ''free'' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ')' 

I used a helper and it works like a charm:

function premium_member()
{
	return auth()->check() &&
		(auth()->user()->sparkPlan()->id === 'monthly'
			|| auth()->user()->sparkPlan()->id === 'yearly'
			|| admin());
}

@Braunson
Copy link

Braunson commented Apr 30, 2019

You can also use this..

Blade::if('plan', function($plans) {
    $plans = is_array($plans) ? $plans : array_wrap($plans);
    $model = \Spark::usesTeams() ? auth()->user()->currentTeam() : auth()->user();
    $activePlan = $model->sparkPlan()->id;

    // Check if the plan is in the array or not
    if (in_array($activePlan, $plans)) {
        return true;
    }

    return false;
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment