Search code examples
laravellaravel-5laravel-bladeboolean-operations

function in a model that returns a boolean fails to return anything in a blade view if 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() }}

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.


Solution

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