1

I use the Laravel Collective system to build my forms and I am trying to populate my formfields with the values that were submitted after the page reloads like this:

{{ Form::text('id', {{ request('id') }} , array('class' => 'form-control')) }}

This throws this error:

syntax error, unexpected '{'

Michael
  • 556
  • 1
  • 8
  • 27

2 Answers2

0

old('id') Should return the input named id from last request.

So in your case:

{{ Form::text('id', old('id') , array('class' => 'form-control')) }}

user3647971
  • 1,069
  • 1
  • 6
  • 13
0

The problem is that you're using blade inside blade:

\/                  \/
{{ Form::text('id', {{ request('id') }} , array('class' => 'form-control')) }}

And this is not accepted obviously.

You must keep in mind that once you open a blade tag {{, the blade will translate it to this:

<?php echo Form::text('id', {{ request('id') }} , array('class' => 'form-control')) ?>

Note that it doesn't work recursively, so the second level blade tag will not be translated, and { is a invalid character inside a PHP code.

To solve it, you shouldn't (and needn't) use the second level blade tag:

{{ Form::text('id', request('id'), array('class' => 'form-control')) }}

Of course, I'm assuming that the request() function exists, otherwise it will throw an error: Call to undefined function request().


To solve your problem (not the error), you should use the old() method instead of request(), since it uses the value stored by Laravel on session.

Note that this approach works in two situations:

  • When a validation error occurs.
  • When you have flashed the fields manually.

This behaviors are described here on Laravel Docs

Md. Khairul Hasan
  • 704
  • 1
  • 10
  • 21
Elias Soares
  • 9,884
  • 4
  • 29
  • 59
  • Yes, that is the error. But not what he asked to be honest. – user3647971 Oct 22 '18 at 13:37
  • The question itself is confusing, he starts asking something, then show a piece of code that don't work throwing that error. I explained how to fix that error, but if the logic don't work, that's another problem. – Elias Soares Oct 22 '18 at 13:44
  • Title is self-explanatory, but it's true that the question remains vague. – user3647971 Oct 22 '18 at 13:45