php Laravel Blade 如何使用 if 语句为空或空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30244592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Blade how to use if statement for null or empty
提问by paradox
In the Blade templating engine, how to use "if" to determine null or empty?
在 Blade 模板引擎中,如何使用“if”来确定 null 或 empty?
{{{ Auth::user()->age }}}
{{{ Auth::user()->age }}}
回答by Vishal Wadhawan
You can do it as bellow
你可以这样做
@if (empty(Auth::user()->age))
// your if code
@else
// your else code
@endif
回答by Jorge Arimany
you can also use:
您还可以使用:
{{ Auth::user()->age or 'Age not provided' }}
so you will have more clean blade template
所以你会有更干净的刀片模板
回答by Razi Kallayi
This works for me:
这对我有用:
{ Auth::user()->age ?: 'Age not provided' }}
{ Auth::user()->age ?: 'Age not provided' }}
回答by arthurborges
In my case, as I was using HeidiSQL as a DB Manager, I had to guarantee that the column field was really NULL. To do that, I clicked on the field with the right mouse button, clicked on "Insert value", then on "NULL". (It's the same as Shift+Ctrl+N). In the beginning, I was just entering NULL on the field, which is a String I guess, so it was always "truthy", not NULL. Doing that solved the problem for me, even writing {{ Auth::user()->age }}
.
就我而言,当我使用 HeidiSQL 作为数据库管理器时,我必须保证列字段确实为 NULL。为此,我用鼠标右键单击该字段,单击“插入值”,然后单击“NULL”。(它与 Shift+Ctrl+N 相同)。一开始,我只是在字段上输入 NULL,我猜这是一个字符串,所以它总是“真实”,而不是 NULL。这样做为我解决了问题,甚至编写{{ Auth::user()->age }}
.