返回布尔值的模型中的函数如果为false则无法在刀片视图中返回任何内容 [英] function in a model that returns a boolean fails to return anything in a blade view if false
问题描述
我在模型中有一个函数,可以返回true
或false
.
I have a function in a model that can return either true
or false
.
我在视图中使用此函数,当我调用它时发现的第一件事是
I use this function inside a view and first thing I discovered when I called it is
{{ Setting::isDesktop() }}
如果我在刀片文件中执行此操作,它将输出1而不是true
.如果我执行dd({{ Setting::isDesktop() }})
,则它将打印true
或false
.
that it outputs 1 instead of true
if I do that inside blade file. If I do dd({{ Setting::isDesktop() }})
then it will print true
or false
.
第二个问题是,如果值为false
,则从刀片文件执行此操作时不会打印任何内容.我需要打印一些1/0
或true/false
Second thing that is giving me a problem is that, if the value is false
then nothing is printed when doing this from blade file. I need something to be printed either 1/0
or true/false
为什么布尔值转换为刀片文件中的数字而不转换为控制器中的数字? isDesktop返回false
时如何打印?现在,在这种情况下,它什么也不打印.
Why does boolean get converted to number inside blade files but not controllers? How can I get something printed when isDesktop returns false
? Right now it prints nothing in that case.
推荐答案
有几种方法可以满足您的需求.
这是其中的几个,
1.使用如下所示的三元运算符,
There are few methods you can achieve your requirement.
Here are couple of them,
1. Use Ternary operator like below,
{{ Setting::isDesktop() ? 'true' : 'false' }}
2.如下所示使用var_export,
2. Use var_export as shown below,
{{ var_export(Setting::isDesktop()) }}
这篇关于返回布尔值的模型中的函数如果为false则无法在刀片视图中返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!