Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shaddark's avatar

Laravel 9 - Keep last insert of input value after submit

Hello, i'm reproducing an old site with Laravel and i have this behaviour to do

About the logic:

Let's say i have 3 controllers for OwnerInfo, StoreInfo, StatInfo. Each of them return an index view. The logic there is simple, just extract data from database and return to the view.

In my app.blade i'm using a "search form" partial component. In that input field the user inserts an ID that will be passed to the controller and used as the param for extract data. This component is visible for each mentioned views before. So basically it's one "search" for all views. I search for ID 5, with $request->input('id') i get it.

So i look for id 5 in OwnerInfo view and get all related data. Then when i want to go in StoreInfo view i need that the fields are already filled with data of ID 5.. And so for the other view. The problem is that after submit or page refresh the input value resets to null. I gave a look on session in laravel documentation and flash data, but i don't really understand on how to do it.

My code:

// search.blade.php

<nav class="navbar navbar-light">
    <form class="form-inline position-relative" method="GET">
    @method('GET')
      <input name="id" id="id" class="form-control shadow-none" type="number" placeholder="Code..."  value="{{ 	request()->input('id')}}">
      <button id="search-btn" type="submit" class="btn btn-light search-btn"><i class="fas fa-search"></i></button>
    </form>
</nav>
// app.blade.php

@include('partials.search') // search input

@yield('content') // any mentioned view

AnyController.php

public function index(Request $request)
{
	$id = $request->input('id');

	$data = DB::table('store_info')->where('id', $id)->get();

    return view('controller.index', compact('data'));
}
0 likes
5 replies
tisuchi's avatar

@shaddark I think you can keep the last insert of input value after submitting by using old function in your input fields.

<input name="id" id="id" class="form-control shadow-none" type="number" placeholder="Code..."  value="{{ old('id', request()->input('id'))}}">

This will first check if there is an old input value in the session and will use it, if not it will check the current request input value.

Additionally, you can also use the session helper function to store the input value in the session for the next request.

In your controller, after the validation and before redirecting:

session(['id' => $request->input('id')]);

And in your blade template:

<input name="id" id="id" class="form-control shadow-none" type="number" placeholder="Code..."  value="{{ old('id', session('id'))}}">

This way, even if the user refreshes the page, the input value will still be there and you can keep it as long as you need.

1 like
shaddark's avatar

@tisuchi I understand what you mean but this isn't really working.. i added session(['id' => $request->input('id')]); before controllers redirect and {{ old('id', session('id'))}} to input value but..

if i'm on page:

http://127.0.0.1:8000/anagrafica

then i search data of given id from the input, the url changes in:

http://127.0.0.1:8000/anagrafica?_method=GET&id=1

When i go to:

http://127.0.0.1:8000/contratti

I need this url -> http://127.0.0.1:8000/contratti?_method=GET&id=1

or at least http://127.0.0.1:8000/contratti but with old('id') inside the input

shaddark's avatar
shaddark
OP
Best Answer
Level 1

@tisuchi Hello i solved it like this:

In every controller i add this code:

        $id = $request->input('id');

        if (session()->has('id') && is_null($request->input('id'))) {
            $id= session('id');
        } else {
            $id= $request->input('id');
            session()->put('id', $request->input('id'));
        }

In the value field of form in search.blade.php:

value="{{ is_null(request()->input('id')) ? session('id') : request()->input('id') }}"

This solution fits perfectly because after submit (page reload) it keeps the value. The only negative point is that i have to use that little code for each controller.

Thanks anyway!

jaseofspades88's avatar

You shouldn't need to bother with the session. If you're sending data to a controller via a GET request then the data is available in the controller for you to do with as you please.

If you visit www.mysite.co.uk?id=12 then you'll have access to $request->id in your controller. What's the problem?

In your view you can simply output values from the query string into the view...

<input type="text" value="{{ request('id') }} />
jaseofspades88's avatar

old('id') is best used for validated data that has failed validation and you've been redirected back via Laravel's native validation

Please or to participate in this conversation.