Laravel 5.1 @can,如何使用 OR 子句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34188461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.1 @can, how use OR clause
提问by Marcaum54
I did not find how to use a clause (OR, AND) in view with @can, for checking multiple abilities ...
我没有找到如何在@can 中使用子句(OR、AND)来检查多种能力......
I tried:
我试过:
@can(['permission1', 'permission2'])
@can('permission1' or 'permission2')
@can('permission1' || 'permission2')
But dont work ;(
但不工作;(
回答by tommy
You can use the Gate facade:
您可以使用 Gate 门面:
@if(Gate::check('permission1') || Gate::check('permission2'))
@endif
回答by Jasper Briers
The @canany blade directive has been added to Laravel v.5.6.23on May 24, 2018
@canany 刀片指令已于2018 年 5 月 24 日添加到Laravel v.5.6.23
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
回答by SimonDepelchin
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.
我已经在我的 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; ?>';
});
Example in blade view:
刀片视图中的示例:
@canany('write|create')
...
@endcanany
Here's the doc for extending Blade on 5.4
回答by jfadich
You can call @can
multiple times.
您可以@can
多次调用。
@if( @can('permission1') || @can('permission2') )
@if( @can('permission1') || @can('permission2') )
@if( Gate::check('permission1') || Gate::check('permission2') )
回答by dilbadil
Use can
method on Authenticated User
,
使用can
方法Authenticated User
,
@if ( Auth::user()->can('permission1', App\Model::class) || Auth::user()->can('permission2', App\Model::class) )
@endif
回答by dipenparmar12
If you are using laravel spatie package
, then you can do it easily by following way..
如果您使用的是 laravel spatie package
,那么您可以通过以下方式轻松完成..
Using hasAnyPermission() method
使用 hasAnyPermission() 方法
@if(auth()->user()->hasAnyPermission(['permission1','permission2']))
// statements
@endif