Laravel 5.1 @can, how use OR clause
36,204
Solution 1
You can use the Gate facade:
@if(Gate::check('permission1') || Gate::check('permission2'))
@endif
Solution 2
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
Solution 3
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
Solution 4
You can call @can
multiple times.
@if( @can('permission1') || @can('permission2') )
@if( Gate::check('permission1') || Gate::check('permission2') )
Solution 5
Use can
method on Authenticated User
,
@if ( Auth::user()->can('permission1', App\Model::class) || Auth::user()->can('permission2', App\Model::class) )
@endif
Author by
Marcaum54
Updated on February 03, 2021Comments
-
Marcaum54 over 3 years
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 ;(
-
jfadich over 8 years@Marcaum54 What about it doesn't work? Does it give an error?
-
Marcaum54 over 8 yearsFollow error: FatalErrorException in /.../0681985e98a64fda6c3db7d157508293 line 53: Call to undefined function can() when generating the view he returned this: <?php if( @can('inscricoes') || @can('inscricoes-opec') ): ?>
-
tommy over 8 yearsThis compiles to
<?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. -
LorenzoBerti almost 7 yearsIs there difference from @can and this way? in this case, wich?
-
titleistfour over 6 yearsVery nice! This should be in Laravel core.
-
Scott Salisbury over 5 yearsDo you know if @ canany supports passing the policy model? For example with @ can you can do @ can('edit', 'App\Post').
-
Clément Baconnier about 5 years@ScottSalisbury you can use e.g.
@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. -
Zsolt Meszaros over 3 yearsPlease, don't post code without an explanation as an answer. Try to explain what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.