1

I am having trouble with developing a Laravel application. When I set a session from a function and loaded the view the session will show:

Session::flash("test","ABC");

return view('layout.customer');

But when I set a session and redirect it to a URL in that page, the session will not work. I am currently using following code:

Session::flash("test","ABC");

return redirect()->route('customer.details');
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
nuz123
  • 11
  • 1
  • 5

3 Answers3

2

Variables that are flashed only last one request.

When you send a redirect, the redirect itself is the first request sent to the visitor. On this request the existing flash session is cleared.
Now a second requests starts for the page that is being redirected to and the session flashes no longer exist.


Update:

You can use the with function to add flashed data to a redirect.

return redirect()->route('customer.details')->with("test","ABC");

More info: https://laravel.com/docs/5.7/redirects#redirecting-with-flashed-session-data

Jerodev
  • 32,252
  • 11
  • 87
  • 108
  • I need to take the success message after a form submission what is the best approach to do it – nuz123 Dec 19 '18 at 12:46
  • 1
    I tried with that one as well it also becomes empty when i try to print on the view – nuz123 Dec 19 '18 at 12:48
  • 1
    Can you try to use `session('test')` instead of `session()->get('test')`? See https://laravel.com/docs/5.7/session#retrieving-data – Matthias S Dec 19 '18 at 13:34
0

You can use return redirect()->route('customer.details')->with("test","ABC");to flash the data when redirecting. Here's the documentation for this: https://laravel.com/docs/5.7/redirects#redirecting-with-flashed-session-data

Sander
  • 209
  • 2
  • 6
-1

In customer.details controller add line use Session; at top of the page.

amit rawat
  • 611
  • 1
  • 5
  • 15