0

I've recently used array notation when naming html input fields. e.g.

<input type="text" name="user[$userId][licenseStatus]">

I've never used this syntax before and although it is extremely handy I cannot see a good way to access the data held in session from the view when using Laravel.

For instance I might want to retrieve the old data back into the input like this when say a validation failure occurs:

<input type="text" name="user[$userId][licenseStatus]" value="{{session()->getOldInput(user[$userId][licenseStatus], '')}}">

But this obviously doesn't work because the array syntax on the name field means the data is held in an array in session like this:

[
_old_input => user[
              32=>licenseStatus = 'xyx',
              12=>licenseStatus = 'xyz'
             ]
]

So is there a smart way to retrieve old input values?

Thanks,

1 Answers1

1

If validation error occurs, in the controller do a redirect with input values. E.g.:

return redirect('form')->withInput();

Then in the form itself, you can put the form value like this:

<input type="text" name="user[$userId][licenseStatus]" value="{{ old('user.$userId.licenseStatus') }}">

You can double check in the laravel documentation: https://laravel.com/docs/8.x/requests#flashing-input-then-redirecting

aceraven777
  • 4,358
  • 3
  • 31
  • 55