3

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?

Siyual
  • 16,415
  • 8
  • 44
  • 58
thmspl
  • 2,437
  • 3
  • 22
  • 48

2 Answers2

2

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.

Community
  • 1
  • 1
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • Thank you for your answer! But unfortunately it isn't working with "{{\Illuminate\Support\Facades\Session::get("hello")}}" – thmspl Sep 16 '16 at 12:26
  • Try just `{{ Session::get('hello') }}`, also if you're using latest 5.2 (5.2.27 and higher) try to remove `web` middleware from `routes.php`, because it applies automatically. – Alexey Mezenin Sep 16 '16 at 12:31
  • @thmspl You can use the `session('hello')` helper method. Or `session()->get('hello')` – Jonathan Sep 16 '16 at 14:36
1

The problem is the use of web middleware:

Route::group(['middleware' => ['web']], function() {

By default this middleware is applied for the entire application. So, calling it again just corrupts the flash and session data.

manix
  • 14,537
  • 11
  • 70
  • 107