Session in Laravel 5

Asked

Viewed 654 times

1

In my controller, log in as follows:

if(!empty($input['user'])) {
            $result = $this->userRepository->searchuser($input['user']); //Busca o usuario pelo login
            // dd($result['data']['user']);

            if ($result['success']) {
                $request->session()->put('user', $result['data']['user']); //Armazeno os dados do usuario numa sessão

                if (Auth::loginUsingId($result['data']['user']->id)) {
                    return redirect('/');//faz o login e redireciona para a rota
                }
            } 
        }
    }
    return view('web::login');

And in another controller I recover the value of the Session:

if($request->session()->has('user')) {
        $user = $request->session()->get('user');
    }        

    return view ('web::profile')->with(compact('user'));

The problem is that sometimes the value is saved and sometimes not, hence when I recover the value in the view, it gives error for not finding the session data. Another problem is that when it works and it can recover the values of Session, after I click on some link of a route (<a href="/home"></a>), the session expires. Some Jedi can help me?

1 answer

2

You can use Laravel’s own class to retrieve authenticated user data.

\Auth::user()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.