abdul rashid
abdul rashid

Reputation: 750

Laravel Session access through Middleware Methods - Session store not set on request

I am setting a session during auth/register (Laravel Signup Auth) functionality. When I try to access the session my_user_referral_session I set during auth/register in the method which is executed immediately after Signup.

I am getting following error

RuntimeException in Request.php line 870:
Session store not set on request.

My Routes.php file

Route::get("/dashboard","DashboardController@dashboard");

DashboardController.php

public function __construct() {
    $this->middleware('auth');
} 

public function dashboard() {
    $request = new \Illuminate\Http\Request();

    if($request->session()->has("my_user_referral_session")){
     /* Reading session data set during Laravel auth/register 
    }

enter image description here

Note: When I use session() helper method it works without any error, but I need to remove the session , which is not in session() helper methods.

Notice: $this->middleware('auth'); in the construct. I suspect this is causing issue

Upvotes: 0

Views: 1482

Answers (2)

Elisio L Leonardo
Elisio L Leonardo

Reputation: 249

I think the line

$request = new \Illuminate\Http\Request();

creates a new "empty" request, so there is no session data there.

The best way to get the session data there is to use the session() helper:

session('my_user_referral_session')

will return your required information or null if there is no session data with that key.

Upvotes: 0

Chris
Chris

Reputation: 3795

You can remove the session with session()->forget('my_user_referral_session');

Session is not bound to the request.

Upvotes: 1

Related Questions