thmspl thmspl - 2 months ago 9
PHP Question

Laravel Redirect->with(key, val) not working

I would like to send Informations from one function to an another with the

->with(key, val)
function, but it doesn't work. I've tried soo many things but it doesn't work.

Here my actual setup (I'm using laravel 5.2):

routes.php



Route::group(['middleware' => ['web']], function() {
Route::get("/test1", "InfoController@test1");
Route::get("/test2", "InfoController@test2");
});


InfoController.php



class InfoController extends Controller
{
public function test1(){
return View::make("infos");
}

public function test2(){
return Redirect::to("/test1")->with("hello", "world");
}
}


Infos.blade.php



{{\Illuminate\Support\Facades\Session::get("hello")}}


The sit empty -> no output.

Where is the problem?

Answer

with() passes session data, not a variable. So you need to use session get() method to get the data:

{{ Session::get('hello') }}

Also, if you're using 5.2.27 or higher, remove web middleware to make sessions work.

Comments