Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange issue with Laravel's flash session

Tags:

php

laravel

I'm encountering a very strange issue and couldn't find anything about it. I'm trying a very simple thing, using a flash message in Laravel to display a message to the user if a condition didn't met.

Basically this is the controller code i'm using:

        Session::flash('error', 'error message');
        return redirect()->back();

//      this didn't work either
//      return redirect()->action('Controller@method', $var)->with('error', ['error message']);

The thing is, it IS working when I var_dump the session in the view, but not without it. This is the view:

{{ Session::get('error') }} // does not work
{{ dd(Session::get('error')) }} // works!

It is a very simple thing, but I don't know why it goes wrong.

Any helpers around? :) Thanks!

like image 269
Shay Avatar asked Nov 02 '25 02:11

Shay


1 Answers

You shall debug in this way

Clear all the files in your sessions folder

In your controller you can create flash message using any of the given two ways

 Session::flash('error', 'error message');
 return redirect()->back();

or

  return redirect()->action('Controller@method', $var)->with('error', ['error message']);

Then in your blade

@if (Session::has('error')) 
<div class="alert alert-info">
    {{ Session::get('error') }}
</div> 
@endif
like image 134
Sulthan Allaudeen Avatar answered Nov 03 '25 16:11

Sulthan Allaudeen