Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

User1980's avatar

Can include() be used the other way around?

Hi all,

I have 4 roles in my app, during loops I would like to check my data like this: //if role includes admin or super-admin or manager if(item.role.includes(['admin', 'super-admin', 'manager'])){ //do this }

//If role includes user if(item.role.includes(['user'])){ //do that }

Would this be the correct way to check if one of the given role exists in a loop?

Thank you.

0 likes
4 replies
Sinnbeck's avatar

Be aware that checking permissions in Javascript isn't safe. I don't know if that is what you are doing but just a heads up

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

I think I understand what you are after. You can invert the if statement with a !

if(! item.role.includes(['admin', 'super-admin', 'manager'])){
1 like
User1980's avatar

@Sinnbeck Thanks, Yes I understand it is not safe but in this case I only use it to show my results in a specific order(in a chat room),either on the left or right side of the screen when the reply is received by an admin or a user. Anything related to sensitive information is done on the Laravel side(I have roles and permissions in the constructors)

User1980's avatar

@Sinnbeck Just to let you know. I have tried your solution, but it looks like includes() in javascript will not check from an array of results. Instead, I had to do this(not ideal as it is a bit long and repetitive):

if(item.role.includes(['admin']) || item.role.includes(['super-admin']) || item.role.includes(['manager'])){
}

Please or to participate in this conversation.