Laravel HTML Collective prefill/persist data for GET request submissionNovember 21, 2019
LaravelCollective is one of the most popular package in the Laravel community. I myself is a big fan of it and I always use the package in almost all of my projects. The best thing about the package is, it'll reduce development time to greater extent like prefilling the data on form fields incase of any errors, adding necessary csrf fields and _method fields accordingly.
Recently while working on one of my office projects, I came across a scenario where I had to persist the form filled data for GET request and it wasn't working as expected. Basically I was trying something like this
I had used the laravel form collective for generating the form fields and when I submitted the form through GET request, it would hit my server and return with results along with prefilling the submitted form data. Even though I got results rendered properly, my form fields were not prefilled with submitted data. So I started searching why it was not prefilling across internet and I couldn't a proper solution for it. So I decided to roll up my sleeves and built the functionality myself.
Firstly, I started with diving deep into LaravelCollective package
understand how the data binding works under the hood for form
fields. After deep inspection I found that laravel maintains a
_old_input key in the session for old inputs and
laravel collective package makes use of it and prefills the data in
the form fields. However, this will work only if the request is
Thats it! By allocating the data on _old_input, laravel collective prefilled the data after form submission and laravel will automatically clear the _old_input session key after each successful request.