Zizaco / entrust

Role-based Permissions for Laravel 5
MIT License
6.05k stars 1.29k forks source link

Can I check 2 role in blade? #934

Open abuzamanbd opened 5 years ago

abuzamanbd commented 5 years ago

Hi, I want to show some specific menu 2 users. But If I set like blew code it's not working. Can anyone have any suggestion, How can I set a menu for 2 different roles?

@role('admin','host')
    <p>This is visible to users with the admin role. Gets translated to 
    \Entrust::role('admin')</p>
@endrole
mazeeblanke commented 5 years ago

@abuzamanbd the blade directive "@role" translates back to <?php if (\Entrust::hasRole({$expression})) : ?>

so in order to get the functionality you are requesting for, you will need to register a new blade directive in a service provider for it

yocmen commented 5 years ago

        Blade::directive('role', function($expression) {
                return "<?php if (\\Entrust::hasRole{$expression}) : ?>";
        });
        Blade::directive('endrole', function($expression) {
            return "<?php endif; // Entrust::hasRole ?>";
        });
        // Call to Entrust::can
        Blade::directive('permission', function($expression) {
            return "<?php if (\\Entrust::can{$expression}) : ?>";
        });
        Blade::directive('endpermission', function($expression) {
            return "<?php endif; // Entrust::can ?>";
        });
        // Call to Entrust::ability
        Blade::directive('ability', function($expression) {
            return "<?php if (\\Entrust::ability{$expression}) : ?>";
        });
        Blade::directive('endability', function($expression) {
            return "<?php endif; // Entrust::ability ?>";
        });```

$expression = Array or String