I'm trying to hide some element and it seems that only the 2nd statement is working
1st
@if(!auth()->user()->role_id == 1) hidden @endif
2nd
@if(auth()->user()->role_id != 1) hidden @endif
CodePudding user response:
The issue is because of the order of operations. You can see the operator precedence in PHP's documentation here.
The "not" operator (!) has a higher precedence than the comparison operators, so it is evaluated before the comparison is made.
In other words, your first condition is actually equivalent to:
// note the parentheses to show explicit execution order
(!auth()->user()->role_id) == 1
So, you're actually "notting" the role_id first, and then comparing that result to the number 1. If your role_id is anything other than 0, the condition will be false. If your role_id is 0, then your condition will be true (due to the loose comparison operator used).
If you want your first statement to work, you'll need to wrap the comparison in parentheses, and then "not" the result:
@if(!(auth()->user()->role_id == 1)) hidden @endif
CodePudding user response:
it's happend in only laravel blade. otherewise both work on core php or in laravel controller. thanks