我没有找到如何在@can视图中使用子句(OR,AND)来检查多个异能......
我试过了:
@can(['permission1', 'permission2'])
@can('permission1' or 'permission2')
@can('permission1' || 'permission2')
但是不行;(
您可以使用门面:
@if(Gate::check('permission1') || Gate::check('permission2'))
@endif
@canany刀片指令已于2018年5月24日添加到Laravel v.5.6.23
用法:
@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
我在我的Laravel 5.4应用程序中添加了这个指令,允许我在我的刀片视图中使用新的@canany('write|delete')
指令。
// 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; ?>';
});
刀片视图中的示例:
@canany('write|create')
...
@endcanany
你可以多次打电话给@can
。
@if(@can('permission1')|| @can('permission2'))
@if( Gate::check('permission1') || Gate::check('permission2') )
在can
上使用Authenticated User
方法,
@if ( Auth::user()->can('permission1', App\Model::class) || Auth::user()->can('permission2', App\Model::class) )
@endif