Laravel 5.1 @can, how use OR clause
Asked Answered
I

8

45

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 ;(

Intended answered 9/12, 2015 at 20:34 Comment(0)
S
48

You can use the Gate facade:

@if(Gate::check('permission1') || Gate::check('permission2'))

@endif
Stifling answered 9/12, 2015 at 21:7 Comment(1)
Is there difference from @can and this way? in this case, wich?Plagioclase
A
52

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
Abeu answered 19/7, 2018 at 20:23 Comment(2)
Do you know if @ canany supports passing the policy model? For example with @ can you can do @ can('edit', 'App\Post').Hypnoanalysis
@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.Taxaceous
S
48

You can use the Gate facade:

@if(Gate::check('permission1') || Gate::check('permission2'))

@endif
Stifling answered 9/12, 2015 at 21:7 Comment(1)
Is there difference from @can and this way? in this case, wich?Plagioclase
F
8

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

Feel answered 16/8, 2017 at 10:26 Comment(1)
Very nice! This should be in Laravel core.Socialization
L
4

You can call @can multiple times.

@if( @can('permission1') || @can('permission2') )

@if( Gate::check('permission1') || Gate::check('permission2') )
Luanaluanda answered 9/12, 2015 at 20:56 Comment(3)
@Intended What about it doesn't work? Does it give an error?Luanaluanda
Follow 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') ): ?>Intended
This 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.Stifling
I
3

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

Initiation answered 6/3, 2020 at 15:51 Comment(0)
E
2

Use can method on Authenticated User,

@if ( Auth::user()->can('permission1', App\Model::class) || Auth::user()->can('permission2',  App\Model::class) )

@endif
Exceptionable answered 10/3, 2019 at 2:32 Comment(0)
M
0

@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

Madian answered 19/1, 2021 at 11:53 Comment(0)
E
-1

just use @canany(['permision1','permision2'])

Eckart answered 2/2, 2021 at 12:49 Comment(1)
Please, 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.Mosqueda

© 2022 - 2024 — McMap. All rights reserved.