1

I am creating session in model using service-provider but when i try to fetch session value on another page it will show nothing.

Service Provider (Projectfunction.php)

public function test1(){
    $student_model = new \App\Config();
    return $student_model->createSession();
}
public function test2(){
    $student_model = new \App\Config();
    print_r($student_model->viewSession_v1());
}

Model (Config.php)

public function createSession(){
    session()->put('email', 'abcd123');
}
public function viewSession_v1(){
    $data = session()->get('email');
    return $data;
}

From controller i either use project_function()->test1() or project_function()->test2().

When i call these two separately test2() will not display any session value, but if i call test2() from test1() then it will display session value. i don't know why. is there any rule to create session from controller only? Because i tried to do that from several controllers and it's working fine. (i had created separate display session code in different controller and single session create code in single controller and it's working fine so why it's not working in model and service-provider?)

3
  • What session driver are you using? Commented Jan 15, 2019 at 13:45
  • file. i also check the location but when i call model function there is no session file created but when i tried from controller there is session file so i don't know why it's not working. Commented Jan 16, 2019 at 4:00
  • Instead of session()->put('email', 'abcd123'); use global helper, session(['email' => 'abcd123']); and use session('email') to retrieve the value. Check if this has different results. Commented Jan 16, 2019 at 6:50

3 Answers 3

1

The code below creates a session and works for me.

\Session::put('email', '[email protected]');
\Session::save();
Sign up to request clarification or add additional context in comments.

Comments

0

If you call from test1() to test2() then session() has email value because it made by put('email', 'abcd123') method from test1(), but just access from test2() then it has not email value.

try like this(but i'm not tested in my environment)

public function createSession(){
    $test = session()->put('email', 'abcd123');
    return self::viewsession_v1($test);
}
public function viewSession_v1($test){
    $data = $test->get('email');
    return $data;
}

i hope this helps you :)

1 Comment

No i don't need to call viewSession_v1 from createSession. It was only test case to identify problem.
0

It seems you can't access session in a Service Provider. Check the following link. https://github.com/laravel/framework/pull/7933

You will have to create a Middleware for this purpose. Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.