Eduardo Rafael
Eduardo Rafael

Reputation: 81

Multiple IF in Laravel HasRole

Hello I have an if that validates the access to the user according to the Role Currently I have it like this but I want to change the access to 2 roles.

My original variable is this:

if ($user->hasRole('Delivery Guy')) { .... rest of the code.

I need that if to be something like this, try this but it doesn't work.

if ($user->hasRole('Delivery Guy' || 'Store Owner')) {

I need that if to allow access with both roles.

Thanks!

Upvotes: 0

Views: 2174

Answers (3)

Rajen Trivedi
Rajen Trivedi

Reputation: 1302

You can use any of the following solutions.

$user->hasRole(['Delivery Guy', 'Store Owner']);

$user->hasAnyRole(['Delivery Guy', 'Store Owner']);

$user->hasAnyRole('Delivery Guy', 'Store Owner');

Read More about Roles/Permissions

Hope this will be useful.

Upvotes: 2

Tako 112
Tako 112

Reputation: 1

Use this code

if ($user->hasRole(['Delivery Guy', 'Store Owner'])) {

}

OR

if ($user->hasRole('Delivery Guy') || $user->hasRole('Store Owner')) {

}

Upvotes: -1

Eduardo Rafael
Eduardo Rafael

Reputation: 81

I found the solution thanks

if ($user->hasRole(['Delivery Guy', 'Store Owner'])) {

Upvotes: 1

Related Questions