@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
@dillinghamio that's great for the array! I tried myself with the
in_array()
method and couldn't get it to work ;-) I love your solution better!