Blade default value Laravel 5.2

Asked

Viewed 145 times

4

I have the following code

<strong class="primary-font">Complemento</strong>
<p>{{ $produto->complemento or 'Esta produto não tem complemento' }}</p>

I need that if the $product->complement value is empty, the system display the text 'This product has no complement', but this way is not working.

  • Consider taking a look at my answer. I think it’s a way that makes things easier

3 answers

2


If the variable exists but is empty ('') you can do the following:

{{($produto->complemento != '') ? $produto->complemento : 'Este produto não tem complemento'}}

Or:

{{(trim($produto->complemento) != '') ? $produto->complemento : 'Este produto não tem complemento'}}

The or is 'translated' by Laravel as:

if(isset($var)) {
    echo $var;
}
...

Now this means that if the $var is declared, even if it is an empty string, it will always echo this $var. We have to do nodes 'manually' (as far as I know does not exist in Blade this check built-in) the condition

  • tbm ended up doing it this way when I went through it... but because this guy or doesn’t work?

  • It is because it exists. I will edit the question with the explanation @Raylansoares

  • is, by what I understood it kind of gives a boolean return, and not the value of the field as it seems it would be right, because when I do so it returns "1" if it exists if I am not mistaken

  • Exact in native php (which is what Laravel will convert it to) is equivalent to echo ($produto->complemento != '') ? $produto->complemento : 'Este pro...'; . can even test: <?php echo ($produto->complemento != '') ? $produto->complemento : 'Este pro...'; ?> . So yes, that’s right, the return of ($produto->complemento != '') is true or false (boolean)

  • I get it, I just think he could return the real value of the direct variable soon, it would make it a lot easier, but thanks for the @Miguel clarification

  • The real value of the variable in the problem that has been set is "", an empty string @Raylansoares

  • ah yes, agr dropped the plug kkk

Show 2 more comments

0

I believe that the solution presented and marked as accepted will generate a lot of unnecessary code if you want to do this in several lines.

Instead, why not use the operator ?: PHP? I believe that in addition to improving the understanding of the code, you will have less work.

Instead of doing like this:

{{ ($produto->complemento != '') ? $produto->complemento : 'Este produto não tem complemento' }}

It’s easier to do that:

{{ $produto->complemento ?: 'Este produto não tem complemento' }}

If you need to use the trim, you can do so too:

{{ trim($produto->complemento) ?: 'Este produto não tem complemento' }}

See an example, using PHP, in the IDEONE

-1

In Laravel 5.0 or higher is no longer used {{ }} and yes {!! !!}

{!! $produto->complemento or 'Esta produto não tem complemento' !!}
  • So, but I went straight to the 5.2 documentation and there was this, but here it didn’t work either that way or the other

  • Has value in column ? Certainty ?

  • Yes, my bank has the complement field, but a person can register a product without complement. When the user clicks on product details the system calls the detail function by passing the id on the function: public Function details($id) { $church = Church::find($id); Return view('admin/churches/details')->with('church', $church); } which is used by the code <Strong class="Primary-font">Complement</Strong> <p>{{ $church->complement or 'This church has no complement' }</p> but nothing happens

Browser other questions tagged

You are not signed in. Login or sign up in order to post.