Home > database >  Session is not getting saved after page redirect in Laravel Application
Session is not getting saved after page redirect in Laravel Application

Time:05-10

I am redirecting the page after a successful login check and the session is created but the session is not available after page redirection.

My Login Controller:

if(Hash::check($password,$user->password)){
    session()->put("username",$username);
    session()->put("user_id",$user->id);
   //print_r(session()->all());
   return redirect()->route('home');    
}
else{
    return Redirect::to("/login")->withFail('Wrong Passoword entered.');
 }

if I print the session variables, they are visible and have proper values, but when it is redirected to the home page, the session variables are not set. session()->all() is showing empty array.

CodePudding user response:

You need to pass a name for the key like withFail('message' , 'your message')

CodePudding user response:

I suggest you put the complete controller code. Sessions in Laravel only work with the "web" middleware defined in the App\Http\Kernel class. If you make the request from an API, you must use laravel/sanctum or manually start the sessions with the Session facade.

if (!Session::isStarted()) {
    Session::start();
}
  • Related