Laravel 5.1 @can, how use OR clause

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


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

You can call @can multiple times.

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

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

You can use the Gate facade:

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

@endif