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

fakeheal's avatar

Redirecting and session variables explained

I have a master.blade.php layout which is extended by all other views I have. I made a custom filter that redirects using Redirect::guest(route('get.home.index'))->with('guest_popup', true);. But when I get on the home page the variable $guest_popup is undefined. However it's in the session and is accessible using: Session::get('guest_popup'). Why?

0 likes
4 replies
Roxout's avatar

Ahh KK

Redirect::guest(route('get.home.index'))->with('guest_popup', true); <- removed quote mark

can you show the full code for the filter, the undefined issue suggests the key is being created but no value is assigned

try

$gp = true;
Redirect::guest(route('get.home.index'))->with('guest_popup', $gp);

                                                                                                                                                   
1 like
fakeheal's avatar

@Roxout, thank you for pointing it out. Though this is a mistake, because I didn't copy paste the code I have, just tried to explain my situation.

ohffs's avatar

Oddly enough, I ran into this recently. I used to do ->with('success', 'Saved!') then just use/check the $success variable in the view, but the latest project I set up wouldn't see the 'success' variable - so I had to use session('success'). Never bothered to find out if something has changed in the framework - but it's a bit annoying :-/

SaeedPrez's avatar
Level 50

I could be wrong but I thought when you redirect()->with('foo', 'bar') it saves the data to session. If you want the data as a variable in your view, you need to do view()->with('foo', 'bar').

1 like

Please or to participate in this conversation.