Blade OR syntax within Laravel Collective input value

Asked

Viewed 527 times

4

The following input works normally, but when I pass it to the syntax of Laravel Collective, says that the variable $servico does not exist. It does not exist at all, so it should show the value null).

The syntax of OR in the Collective is wrong?

NORMAL INPUT

<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']) !!}

1 answer

4


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

For you to understand better, when you do Blade’s "echo" like this

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

The Blade compiles to

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

But the above syntax is recognized only with the direct use of the tag {{ expressão }}

In your case, you can use a ternary expression directly by checking whether $servico->cliente exists to print the property nome;Otherwise, you print the null

Example:

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

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

$servico->cliente->nome ?? null

Browser other questions tagged

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