Blade OR syntax within the Laravel Collective input value

4

The following input works normally, but when I move it to the Laravel Collective syntax, it says that the $servico variable does not exist. It does not even exist, so it should display the value null ).

The syntax of OR in Collective is wrong?

INPUT NORMAL

<input type="text" name="nome" value="{{ $servico->cliente->nome or null }}" id="cliente" class="form-control" placeholder="Nome do cliente">

INPUT COLLECTIVE

{!! Form::input('text', 'nome', $servico->cliente->nome or null, ['id' => 'cliente', 'class' => 'form-control', 'placeholder' => 'Nome do cliente']) !!}
    
asked by anonymous 05.10.2017 / 13:30

1 answer

4

In Collective, you will have to do with the syntax of PHP, since this syntax of% of Blade% is compiled for a ternary expression with OR .

For you to understand better, when you do the "echo" Blade so

{{ $servico->cliente->nome or null }}

Blade compiles to

<?php echo isset($servico->cliente->nome) ? $servico->cliente->nome : null; ?>

However, the syntax above is only recognized by the direct use of the isset tag

In your case, you can use a ternary expression directly by checking that {{ expressão }} exists to print the $servico->cliente property; otherwise, you print nome

Example:

{!! Form::input('text', 'nome', $servico->cliente ? $servico->cliente->nome : null, ['id'   => 'cliente', 'class' => 'form-control', 'placeholder' => 'Nome do cliente']) !!}

If you are using versions of PHP 7 or higher, you can simplify to

$servico->cliente->nome ?? null
    
05.10.2017 / 13:35