Eloïse's avatar

Laravel-Permission : what's the utility of the guard_name ?

Hello, I'm new to laravel and I'm playing with Spatie's Laravel-Permission package I have 2 questions.

1 -I'm asking myself what's the aim of the guard_name in the different tables ? Is it intended to have a different set of permissions ?

2- In order to protect pages and sometime sections of pages my strategy is to use the middleware to protect some pages to be accessed and to put roles of the user in session, so I could check permission in my blade to protect a section.

Am I right ? Thanks for your help.

1 like
4 replies
bashy's avatar
bashy
Best Answer
Level 65

guard_name is there so you can configure which Laravel guard to use for the check. Use 'web' by default (which is what it adds in if you use Role::create(['name' => 'writer']))

In config/auth.php you will see;

'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
    ],

Here you get 'web' and 'api'. Maybe you want to have roles or permissions that are for 'api' instead of 'web', which is normal HTML front-end.

You can also create other guards that Laravel can read and use those.

Hope this helps.

8 likes
Jonjie's avatar

@bashy What if I want to use sanctum, what guard should I use?

bashy's avatar

@Jonjie Set your own guard up or use whatever is provided. Not used it directly myself.

Also 4 year ago reply.

Eloïse's avatar

Many thanks for your help ! Sure it will be usefull for my project.

Please or to participate in this conversation.