0

I am trying to save some sessions variables but after return laravel does not save the session variable. Where is the problem in my code?

in web.php:

Route::get('/login', function(){
   return view(frontend.login);
 });
 Route::post('/login', function(Request $request){
   $control = "Some sql codes";
   if($control > 0){
       $user = "Some sql codes";
       $request->session()->start();
       $request->session()->put('user_id', $user->id);
       $request->session()->save();
       return view('frontend.logged_in);
   }
   else{
       return view('frontend.index);
   }
});

I know I should use controller but this is a temporary code and it should works fine here(am I right?). And when I do not use "return view('frontend.logged_in);" it saves the session variable but when I try to return it does not saves the session variable.

Note: If I choose 'file' for session driver theese codes are working just fine. But when I try to choose cookie codes doesn't work..

1 Answer 1

2

I believe it is because of the line:

$request->session()->start();

You dont need to manually start the session, Laravel maintains the session for you.

Use following code instead:

if($control > 0){
   $user = "Some sql codes";
   session(['user_id'=>$user->id]);
   return view('frontend.logged_in);
}

See if that works for you.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry I forgot the accept your answer. You were right, when learning phase reading documentation is very important! :)

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.