Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 - Redirect with flash message not working

In my Laravel 4 project, I am trying to set a flash message when redirecting, like so:

return Redirect::route('main')->with('flash', 'Message here.');

(I looked at the following: Laravel 4 - Redirect::route with parameters).

The redirect happens properly. In my 'main' view I've got this code to display the flash message:

@if (Session::has('flash'))
  <div class="flash alert">
    <p>{{ Session::get('flash') }}</p>
  </div>
@endif

But it doesn't work: Session::get('flash') returns an array, not a string. If I insert a call to var_dump(Session::get('flash')), I get this output:

array (size=2)
  'new' => 
    array (size=0)
      empty
  'old' => 
    array (size=1)
      0 => string 'flash' (length=5)

What is happening? It seems like fairly straightforward code...

like image 718
blazej Avatar asked Mar 13 '14 18:03

blazej


1 Answers

When you redirect with data, the data you save will be 'flashed' into session (meaning it will only be available during the next request, see: http://laravel.com/docs/session#flash-data).

I would be wary of using 'flash' as a flash data key, as Laravel stores its flash data in a key already with a name of 'flash'. Try changing your name to something else. You may be confusing Laravel's Session::get as to where it should be attempting to load the data from.

like image 200
Jeff Lambert Avatar answered Nov 07 '22 08:11

Jeff Lambert