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

hanif-king's avatar

in laravel 5.2.45 session message does not work, how to fix

i am working on a project where its coded in laravel 5.2.45 and i need to have session message but it seems not working and i see nothing wrong in my code but why? here is my controller code

$message="something...";

 Session()->flash('message', $message);
        return redirect()->route('soemroute');

here is how i am accessing the message

//first i try it like this as it's working in earlier versions
@if(Session::has('message'))
{!! session('message')!!}
@endif

//then is used this method too but still not working. there is no error message also 
@if(Session::has('message'))
 {!! Session::get('message')!!}
@endif
 

@Snapey , @martinbean

0 likes
9 replies
meeshka's avatar

@hanif-king Possibly,

  1. Check if web middlewareGroup has \Illuminate\Session\Middleware\StartSession::class, inside Kernel.php
  2. Ensure your routes that need access to session are grouped like below
Route::group(['middleware' => 'web'], function () {

    // Your routes...

});
1 like
ksparkar's avatar

Just to debug can you try Session()->put('message', $message) so we can eliminate the possibility of trying to retrieve the data after more than 1 request?

hanif-king's avatar

@meeshka this is web middleware

'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
        ],
hanif-king's avatar

@ksparkar yes i can do that and can get the message too. but how about flash message that only works for a flash message not more?

ksparkar's avatar

@Cronix - I tested using the helper with capital casing and it works! Am I missing something? If casing was the issue wouldn't he get an error thrown on that line?

@hanif-king if you are saying that put() works but flash() doesn't then it means you are trying to retrieve the message more than one request later.

Cronix's avatar

@ksparkar Maybe your operating system isn't case sensitive, but linux sure is, which is most likely what the os will be for the production site. If you don't follow how things are supposed to be, you'll have a hell of a lot of fixing to do once you push it to a production server. Everything will just break everywhere.

Snapey's avatar

is message already used by the framework? Might be worth using a different name

ksparkar's avatar

@Cronix - Yes I do always use the lower case and never have issues in production. Just trying to see what the issue is here for @hanif-king

BTW - PHP documentation does note that function calls are case insensitive - http://php.net/manual/en/functions.user-defined.php but of course it is not a good practice to introduce inconsistency!

He is saying that put() works but flash() doesn't so perhaps he is trying to retrieve the value after more than 1 request?

1 like

Please or to participate in this conversation.