1

So I'm pretty new to Laravel and not at the point where I have mental bandwidth to mess with Vue. In the meantime, I'm also trying to build forms dynamically to make the code easier on the eyes.

My code looks like this:

<form action='{{ route('agents.store') }}' method='POST'>
        @csrf

        @php
            $details = ['prop' => 'labelName', ...];
        @endphp

        @foreach ($details as $data => $label)
            <div class="{{ $data }}">
                <label for="{{ $data }}">
                    {{ $label }}:
                </label>
                <input type="text" name="{{ $data }}" id="{{ $data }}" value="{{ old('{{$data}}') }}">
                @error('{{ $data }}')
                    {{ $message }}
                @enderror
            </div>
        @endforeach

Now, this seems to work fine. I do, however, get an error when I save this and the formatting option of VSC refuses. I assume this is because value="{{ old('{{$data}}') }}"

is basically doubling down on inserting a variable dynamically.

Is there a proper way I can build my form in the meantime (before learning probably Vuejs)? This form takes 15 inputs and it gets pretty messy.

Kind regards in advance.

I've tried old, trusty SO & Google. Amongst other, I found this solid post but trying different apostrophes and ticks just led to variations of error message

Parse Error : syntax error, expecting ')' on line 1 > 1 | | ^

There are apostrophes needed for the old(''), so I've added extra and different types in the old() and the array, but I keep getting slight variations on the same error.

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
Wikke
  • 31
  • 3
  • 2
    `value="{{ old('{{$data}}') }}"` why double `{{}}`? you are already writing php inside the first `{{}}`. Try `value="{{ old($data) }}"` – kris gjika Jul 13 '23 at 07:57
  • 1
    Cheers, @krisgjika I feel a bit silly now but that did the trick. With the documentation example, I had the impression old('username') was expecting the apostrophes to make it function, so that I'd need to double down on creating a variable rather than getting rid of them. Worked like a charm. – Wikke Jul 13 '23 at 08:12

1 Answers1

0

I don't know if it's the proper way to close my question, but with @krisgjika's answer I'm all set...

Wikke
  • 31
  • 3