Ternary in Laravel Blade

…衆ロ難τιáo~ 提交于 2019-12-02 15:24:01

You are free to use it with {{ }}.

{{ Auth::check() ? 'yes' : 'no' }}

This works:

{{ Auth::check() ? 'yes' : 'no' }}
Bryce

I know this question was asked a while ago, but this may help somebody.

You can now do this in Laravel 5.

{{ $variable or "default" }}

Laravel 5 Blade Templates

Laravel 5.2 Blade Template

in addition, here is a nice shortcut ?:, if you you need to print some variable's value or if it's empty some default text

{{ $value ?: 'Default Value' }}

For Laravel 5 + php7, you should be using the null coalesce operator as explained in this Laravel News article, like so:

{{ $value ?? "Fallback" }}

Before the null coalescing operator, Blade handled the same problem with the “or” operator, which allows a default value when the first value isn’t set, separated by an “or”.

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