2

I have a following code in my posts.create action.

// ...
return redirect(route('posts.index'))->with('flashMessage', 'test');
// ...

I expect $flashMessage variable to be available in the posts.index view, however, it is not. What am I doing wrong?

P.S

I don't want to set the flash message using Session::flash('flashMessage', 'test'), because it won't work in case of json responses.

Alex Lomia
  • 6,705
  • 12
  • 53
  • 87

2 Answers2

4

in redirect you have to pass url not route name add the following code to your blade page

@if(Session::has('flashMessage'))
  <div class="alert alert-danger">
      {{ Session::get('flashMessage') }}
  </div>
@endif
sunilwananje
  • 714
  • 5
  • 17
  • The redirecting occurs just fine. There is nothing wrong with the syntax, however the variable is stored in the session indeed, thanks! – Alex Lomia Aug 09 '16 at 12:52
-2

Seems like your syntax is wrong. Try this one

return redirect()->route('posts.index')->with('flashMessage', 'test');
Gadzhev
  • 442
  • 2
  • 6
  • The redirecting occurs just fine. There is nothing wrong with the syntax I use, it's the message that is not passed to the view. Unfortunately, your code doesn't work either – Alex Lomia Aug 09 '16 at 12:47