Reputation: 240
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?)
Upvotes: 1
Views: 220
Reputation: 41
The code below creates a session and works for me.
\Session::put('email', '[email protected]');
\Session::save();
Upvotes: 1
Reputation: 146
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.
Upvotes: 0
Reputation: 1749
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 :)
Upvotes: 0