Joel Joel Binks
Joel Joel Binks

Reputation: 1676

Laravel 5.2.32: Session flash doesn't work with redirect

I have a controller which has a redirect function:

public function myControllerMethod() 
{
    $data = $this->blabla();

    return Redirect::to('previousroute')->with('data', $data);
}

This previousroute is handled by otherControllerMethod() like so:

public function otherControllerMethod()
{
    $data = Session::get('data');

    return $this->makeView($data);
}

Unfortunately, Laravel forgets this session data. I've done this many times before and I have never seen if forget the session flash data after a single redirect. What is going on here? I have tried both adding and removing "web" middleware but nothing works. If anyone knows why this happens let me know.

Upvotes: 0

Views: 536

Answers (3)

Minhaj Mimo
Minhaj Mimo

Reputation: 836

use Session;

public function myControllerMethod() 
{
    $data = $this->blabla();
    Session::set('data', $data);
    return Redirect::to('previousroute')->with('data', $data);
}

public function otherControllerMethod()
{
    $data = Session::get('data');

    return $this->makeView($data);
}

Try like this. Use the session and set the data in session and get it from where you want.

Upvotes: 2

Osama Sayed
Osama Sayed

Reputation: 2023

I have had the same Issue before. Basically I needed to call send function when redirecting using Redirect facade. So you need to change your myControllerMethod to:

public function myControllerMethod() 
{
    $data = $this->blabla();
    return Redirect::to('previousroute')->with('data', $data)->send();
}

As send() function of class Symfony\Component\HttpFoundation\Response calls the function sendContent() which sends the data when redirecting.

Hope this helps.

Upvotes: 1

plicaman
plicaman

Reputation: 31

In myControllerMethod you are passing the data obj/var as a Request.

In otherControllerMethod you are requesting the Session data which is not set.

In order to put data to the session you should do:

Session::put('data','value')

and then it will be available with:

Session::get('data');

Upvotes: 0

Related Questions