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

emkays's avatar

Laravel 5.1 strange session behavior

I am setting few session variables in my search controller as below:

        // Add the search form values to session too
        session()->put('search-keyword', $keyword);
        session()->put('search-geolocation', $geoLocation);

Later,

        return view('search.index', compact('profiles'));

In my search blade template, have this line:

        <div style="background-image:url( '{{ $profile['_source']['picture'] }}' )"></div>

I am using 'file' session driver and the above works very well until $profile['_source']['picture'] is null. When it is null, for some reason session variable search-geolocation is never set! As checked in the session file at storage/framework/sessions: ...."search-geolocation";N;s:9....

If I change the <div> above to <img>, everything again works beautifully, regardless of whether $profile['_source']['picture'] is null or not null:

        <img src="{{ $profile['_source']['picture'] }}" alt="">

This same problem never happens with the session variable search-keyword, whether $profile['_source']['picture'] is null or not null; search-keyword appears as it should in the session file.

Right after setting the session in the controller, I get correct value when I echo:

        echo session()->get('search-geolocation');

Also, this problem goes away when I debug the code (XDebug) and all the values are stored correctly in the session file as expected. So, debug is not of much help.

I have already worked around this problem but I am not confident about session anymore. Therefore, want to understand what's going on here. Have tried changing the session driver without any difference.

0 likes
2 replies
afrayedknot's avatar

If $profile['_source']['picture'] is not set - that might cause a PHP error, and maybe that is resulting in the session not being set?

If you are using PHP7 try this:

     <div style="background-image:url( '{{ $profile['_source']['picture'] ?? null }}' )"></div>

...

emkays's avatar

Thanks Laurence, I did think about the same thing. What I realize is that for some reason a resultant output like

        <div style="background-image:url(  )"></div>

is tripping something (it shouldn't, but looks like it is), because the following works (btw, the variable is set) -

        <div style="background-image:url( {{ empty($profile['_source']['picture']) ? 'Test' : $profile['_source']['picture'] }} )"></div>

(which in the end results into

        <div style="background-image:url( Test )"></div>

for the null values).

I read somewhere that helpers like dd interrupts the session storage. Could it be something similar happening when the blade is being compiled? I don't see any errors though in the logs or otherwise. Also, it baffles me as to why <img> would work regardless and <div> not. There cannot be a connection between php and html syntax but perhaps there is something during blade compilation.

Please or to participate in this conversation.