Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@can blade with guard name not working #1290

Closed
AhmedHdeawy opened this issue Nov 27, 2019 · 12 comments
Closed

@can blade with guard name not working #1290

AhmedHdeawy opened this issue Nov 27, 2019 · 12 comments
Labels

Comments

@AhmedHdeawy
Copy link

Hi, thanks for this awesome package,

i have a problem that is:

i have multiple guards [ 'web', 'admin' ]

in Admin Model i have
protected $guard_name = 'admin';

and i have guard name for each permission in

permissions table

like the image below
p

the problem is when use @can directive like:
@can('admin.users.view')
it doesn't work, not show the code that in it, even if this admin is Super Admin or Admin

also when use
@can('admin.users.view', 'admin')
it doesn't work also

but when use
@if(auth('admin')->user()->can('admin.users.view'))
it works fine

how i can use @can only because is simple for code.

thanks in advance

@drbyte drbyte added the support label Dec 19, 2019
@sameervirus
Copy link

+1

@drbyte
Copy link
Collaborator

drbyte commented Feb 19, 2020

You cannot pass the guard to @can. You must use the method you described, or maybe hasPermissionTo.

@drbyte drbyte closed this as completed Feb 19, 2020
@malickatique
Copy link

Anyone solved this issue?

@drbyte
Copy link
Collaborator

drbyte commented Feb 25, 2020

Nothing to "solve". It's operating by design.

@b8x
Copy link

b8x commented May 8, 2020

@AhmedHdeawy, You can create own blade directive like

        Blade::directive('permission', function ($permission, $guard = "admin") {
            return "<?php if (auth()->user()->hasPermissionTo({$permission} , \"{$guard}\")) { ?>";
        });
        Blade::directive('endpermission', function () {
            return '<?php } ?>';
        });

@MD-Khalid-Hosain
Copy link

MD-Khalid-Hosain commented Nov 2, 2020

Solved my problem... Thank you bro @AhmedHdeawy AhmedHdeawy

@if(auth('admin')->user()->can('admin.users.view'))
it works fine

@jainjii
Copy link

jainjii commented Sep 14, 2022

why can't create own blade directive just like @can('','','')

@Lukmauu
Copy link

Lukmauu commented Sep 6, 2023

@auth('admin')

Works fine if you admin guard set in your config/auth.php

@axlwild
Copy link
Contributor

axlwild commented Oct 12, 2023

Given this problem, I've created a PR to be able to use another blade directive in this case, a little bit more confortable than the whole @if(auth('admin')->user()->can('admin.users.view')) sentence.

With this change, you can use the following directive

@haspermissionto('admin.users.view', 'admin')
   ...
@endhaspermissionto

@nguyencuongcd44
Copy link

The @can does not work with my custom guard so I changed the default guard to my custom guard, and it worked.
Is anyone else experiencing the same issue as me?
How can I use this with custom work ?
Thank you very much.

@parallels999
Copy link
Contributor

parallels999 commented Oct 18, 2024

Did you try auth()->user()->can('admin.users.view', 'YOUR_CUSTOM_GUARD') ?

@nguyencuongcd44
Copy link

It worked! I spent 2 days on this issue. Thank you very much for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests