@role Blade Directive For Laravel Spark

May 9, 2016 ยท View on GitHub

@role Blade Directive For Laravel Spark

Assumes you're using teams

Add this to the boot() method of your AppServiceProvider

\Blade::directive('role', function($roles) {

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

        $userRole = $user->roleOn($user->currentTeam);

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

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

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

And use like so

@role('owner')
    <h1>Owner</h1>
@endrole

@role('member')
    <h1>Member</h1>
@endrole

Can also pass an array of multiple roles

@role(['member', 'owner'])
    <h1>Member or Owner</h1>
@endrole

See also @plan Blade Directive For Laravel Spark