use ternary operator as if elseif and else

冷暖自知 提交于 2020-03-05 06:04:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!