function in a model that returns a boolean fails to return anything in a blade view if false

痞子三分冷 提交于 2020-01-05 04:12:07

问题


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() }}

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.

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

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. Use var_export as shown below,

{{ var_export(Setting::isDesktop()) }}



来源:https://stackoverflow.com/questions/44683219/function-in-a-model-that-returns-a-boolean-fails-to-return-anything-in-a-blade-v

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