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

lemmy's avatar
Level 20

Flashing request issue

I am having a simple issue and wanted to know how others tackle this issue in Laravel. I have a resource route.

The index method facilitates searching using multiple fields.

   public function index()
   {
            $feeTypes = FeeType::addSearchFilters(request()->only(["code", "description"]))
                   ->paginate(10)->withQueryString();

            request()->flash();
    
            return view('feetypes.index', compact('feeTypes'));
   }

feetypes/index.blade.php Is using {{ old('code', '') }} and {{ old('description', '') }} to get the previous values to populate search fields. Additionally, the search results is linking to the feetypes/edit.blade.php which also have the fields: code and description. Whats happening is that the session is leaking over to the GET feetypes/{feetype} route. Normally I just destroy the session at the end of the layout file but I really wonder if there is a better way to deal with this. Also, I think Laravel would have delete the session after the response is sent to the browser.

Whats your thought on this.

0 likes
3 replies
aurawindsurfing's avatar

Hey @lemmy,

If you flash() then this is only persistent for one request.

What you could do is simply store what you need to store in session variable like so: https://laravel.com/docs/master/session#storing-data

I tend to use these days Livewire more for this stuff as it makes of of that flow much more intuitive where you could have I Livewire component with one function responsible for index and search and then one for edit all on the same page.

Hope it helps!

lemmy's avatar
Level 20

Thank you @aurawindsurfing .

The problem is that I am flashing and returning a view. As such, the flashed data is available for the current and the next request. The solution is to flash for the current request only. Laravel provides a nice little helper function for this. seesion()->now($key, $value). Now the issue is how do you flash for the current session in a seem less fashion and ensure that the view either gets the value or null. Here is the solution:

public function index(Request $request)
{
    $feeTypes = FeeType::addSearchFilters($request->only(['code', 'description']))
					->paginate(10)->withQueryString();
    
    session()->now('old', $request->all());
    
    return view('feetypes.index', compact('feeTypes'));
}

Now, the view need to get the value of the session or null.

{{ session('old.code') }}
{{ session('old.description') }}

Hope this helps someone else.

aurawindsurfing's avatar

Nice one!

Did you try Livewire by any chance? Seems like a good case for getting the fields and filters to work.

Please or to participate in this conversation.