I did not find how to use a clause (OR, AND) in view with @can, for checking multiple abilities ...
I tried:
@can(['permission1', 'permission2'])
@can('permission1' or 'permission2')
@can('permission1' || 'permission2')
But dont work ;(
I did not find how to use a clause (OR, AND) in view with @can, for checking multiple abilities ...
I tried:
@can(['permission1', 'permission2'])
@can('permission1' or 'permission2')
@can('permission1' || 'permission2')
But dont work ;(
You can use the Gate facade:
@if(Gate::check('permission1') || Gate::check('permission2'))
@endif
The @canany blade directive has been added to Laravel v.5.6.23 on May 24, 2018
Usage:
@canany(['edit posts', 'delete posts'])
<div class="actions">
@can('edit posts')
<button>Edit post</button>
@endcan
@can('delete posts')
<button>Delete post</button>
@endcan
</div>
@endcanany
@canany(['post.update', 'post.create'], [$post])
But it's not ideal since it's the same argument for both policies. I would recommend to make your own directive for the example I provided. –
Taxaceous You can use the Gate facade:
@if(Gate::check('permission1') || Gate::check('permission2'))
@endif
I've added this directive in my Laravel 5.4 app that allows me to use a new @canany('write|delete')
directive in my blade views.
// AppServiceProvider.php@boot()
Blade::directive('canany', function ($arguments) {
list($permissions, $guard) = explode(',', $arguments.',');
$permissions = explode('|', str_replace('\'', '', $permissions));
$expression = "<?php if(auth({$guard})->check() && ( false";
foreach ($permissions as $permission) {
$expression .= " || auth({$guard})->user()->can('{$permission}')";
}
return $expression . ")): ?>";
});
Blade::directive('endcanany', function () {
return '<?php endif; ?>';
});
Example in blade view:
@canany('write|create')
...
@endcanany
Here's the doc for extending Blade on 5.4
You can call @can
multiple times.
@if( @can('permission1') || @can('permission2') )
@if( Gate::check('permission1') || Gate::check('permission2') )
<?php if(@can('permission1') || @can('permission2')): ?>
. Note that @can()
is a valid function call (the at-symbol suppresses warnings), but the can() method does not exist. –
Stifling If you are using laravel spatie package
, then you can do it easily by following way..
Using hasAnyPermission() method
@if(auth()->user()->hasAnyPermission(['permission1','permission2']))
// statements
@endif
Use can
method on Authenticated User
,
@if ( Auth::user()->can('permission1', App\Model::class) || Auth::user()->can('permission2', App\Model::class) )
@endif
@if(Gate::check('manage-users') || Gate::check('add-new-user')) Manage Users
<li>
<a href="{{url('/back/users')}}">
<i class="feather icon-users"></i>
<span class="menu-item" data-i18n="users">Manage Users</span>
</a>
</li>
@endif
just use @canany(['permision1','permision2'])
© 2022 - 2024 — McMap. All rights reserved.