Ad

Laravel Redirect Route With Variable Not Working

- 1 answer

I'm using Laravel 5.2 and when I use with it doesn't flash the data over.

If I use

Session::flash('test', 'test');

Then it shows the session flash data.

If I put the ->with on the index it also doesn't work either.

Controller

public function store(Request $request)
{
    return Redirect::route('registration::index')->with('test1', 'test');
}

public function index()
{
    return view('registration.index');
}

View:

{{ var_dump(Session::all()) }}

What's going wrong here..?

Ad

Answer

In Laravel 5.2 the StartSession middleware is no longer added to the global $middleware list in the App\Http\Kernel class. Instead it's added to the web middleware group, so the session doesn't start automatically with a request. You have two options to fix this:

1. Add the routes that need to use the session in a route group that uses the web middleware group:

Route::group(['middleware' => ['web']], function () {
    Route::get('/', '[email protected]');
    Route::post('store', '[email protected]');
});

2. Move the middleware from the group to the global middleware list so that the session is started on every request:

protected $middleware = [
    ...
    \Illuminate\Session\Middleware\StartSession::class,
];
Ad
source: stackoverflow.com
Ad