hatemjapo hatemjapo - 25 days ago 21
PHP Question

Laravel form echo variable

i am still learning how to work with laravel.
Now i have learned how to make a form in laravle. But i have some trouble to echo a variable.
What i want to do is: i want to echo a variable as the value of an input-field if this variable exists, otherwise it should echo nothing.
so my form line looks like this

{{Form::text(
'league_name',
'@if(isset($varialble) {{$variable}} @else {{$nothing}} @endif)'
)}}


How can i echo a variable in a form?

i am using blade btw.

Answer

This is the right way to do it:

{!! Form::text('league_name', isset($varialble) ? $variable : '') !!}

If you're using PHP7, you can do this:

{!! Form::text('league_name', $varialble) ?? '') !!}

Update

To add placeholder, pass it in an array as third parameter. Also, you usually want to pass a class:

{!! Form::text('league_name', isset($varialble) ? $variable : '', ['placeholder' => 'My placeholder', 'class' => 'form-control']) !!}
Comments