1

I have a contact form. On submit the POST request goes to a controller that handles the contact form (checks the request and emails the data). At the bottom of the controller I have this:

return back()->with('flash-message', 'Message!');

In the view I try to echo the message with

{{ session('flash-message') }}

This doesn't seem to work. The message is not in the session. What could be wrong? Im using: Laravel version 5.2.7

lagbox
  • 48,571
  • 8
  • 72
  • 83
Rbijker.com
  • 2,894
  • 3
  • 21
  • 26

3 Answers3

4

please take Session variables with this way..

return  redirect()->back()->with('flash-message','message');  

and in View..

 {{Session::get('flash-message')}}
Jishad
  • 2,876
  • 8
  • 33
  • 62
  • No, this is not the issue. I can retrieve Session Variables this way. session('flash-message'). It works when I test it. – Rbijker.com Jan 14 '16 at 12:09
  • This also doesn't work. It does the same as my code. The problem is that the flash data gets lost after the redirect. Can anybody solve this? – Rbijker.com Jan 14 '16 at 18:13
1

I figured it out. It has to do with the Laravel 5.2 update. The middleware which is responsible for making that flash data available to all your views is not being utilized in normal Routes anymore. It was moved from the global middleware to the web middleware group. This post explains the issue and how to fix it.

Laravel 5.2 $errors not appearing in Blade

This post explains 2 ways to fix it:

  1. In your kernel.php file, you can move the middleware \Illuminate\View\Middleware\ShareErrorsFromSession::class back to the protected $middleware property.

  2. You can wrap all your web routes in the web middleware group (see below). Also place the Routes that handle the form here:

Route::group(['middleware' => 'web'], function() { // Place all your web routes here... });

Community
  • 1
  • 1
Rbijker.com
  • 2,894
  • 3
  • 21
  • 26
0

You can do this.In the controller:

Session::flash('message','Empty input not accepted');
return back();

And in the view file to use this Session you can do same as above mentioned:

{{ \Session::get($message) }}

Hope this helps you....

pritesh
  • 2,162
  • 18
  • 24