Store logged in user details in session and display in view in laravel

Updated at 24-Aug-2021, By samar

Store logged in user details in session and display in view in laravel

Good day, guys. In this post, we’ll look at how to solve the "Store logged in user details in session and display in view in laravel" programming puzzle.

You can use the session facade in laravel to store the logged-in user details and display in view file using the get method on session.
  • Store logged in user details using session and display in view in laravel

    <p>Import Auth and Session facade in the controller before definition</p>
    use Illuminate\Support\Facades\Session;
    use Illuminate\Support\Facades\Auth;
    
    //Code in controller's method After login attempt
    $user = Auth::user();
    Session::put('user', $user);
    
    //Display details in view file
    @php $loggedInUser = Session::get('user') @endphp
    //Get id of logged in user
    {{ $loggedInUser->id }}
    

    You can get the logged user details by calling Auth::user() method and you can insert the user's details in the session by calling Session::put() method. Now You can get the user's details in the view file by calling Session::get() method by passing the session name which we have used while creating and get detail by passing the field's name to it.

Back to code snippet queries related laravel