问题
I want to check if the role is agency then the output is $agencyUsers
else if the role is officer then the output is $officeUsers
else I need to output 'No Group'
I try this but the output is 'No Group' for all users
<td>{{ strtolower($user->getRoleNames()) === 'agency' ? $agencyUsers : strtolower($user->getRoleNames()) === 'officer' ? $officeUsers : 'No Group'}}</td>
回答1:
You must wrap the second ternary in parentheses, this should work:
strtolower($user->getRoleNames()) === 'agency' ? $agencyUsers : (strtolower($user->getRoleNames()) === 'officer' ? $officeUsers : 'No Group')
However, I recommend using a different approach. Blade allows for if conditions, which may be more readable than multiple ternaries, or even extracting this logic to a view function.
来源:https://stackoverflow.com/questions/60008199/use-ternary-operator-as-if-elseif-and-else