2 Ways to Get Current Logged in User Info in Laravel

Sovary August 8, 2022 491
3 minutes read

When a user register or logged in successfully, the application will create a session to store current logged in data for user. Sometimes to check user role or some logged info then we need current logged in user data.

In Laravel there are two ways to manipulate current user logged in data, we use Auth facade and auth() helper. So in this tutorial I will show very basic example where you can access user data after logged in. You can call those facade and helper in Controller file and View in below examples.

Get Current Logged in User Info in Laravel

  • Using Helper in Controller
  • Using Facade in Controller
  • Using Helper in View
  • Using Facade in View

Using Helper in Controller

The below controller file using helper class auth() to access user logged in data

<?php
  
namespace App\Http\Controllers;
  
class UserController extends Controller
{

    public function index()
    {
   
        // To see all data in current logged in user
        $u= auth()->user();
        print_r($u);
      
        // To get user id in current logged in data
        $uid= auth()->user()->id; 
        print_r($uid);
          
        // To get user name in current logged in data
        $u_name = auth()->user()->name; 
        print_r($userName);
          
        // To get user email in current logged in data
        $u_email = auth()->user()->email; 
        print_r($u_email);

        // To get user created date in current logged in data
        $u_created = auth()->user()->created_at; 
        print_r($u_created);
    }
}

Using Facade in Controller

How we use facade class named Auth to access user info data in controller file.

<?php
  
namespace App\Http\Controllers;

use Auth;
  
class UserController extends Controller
{

    public function index()
    {
   
        // To see all data in current logged in user
        $u= Auth::user();
        print_r($u);
      
        // To get user id in current logged in data
        $uid= $u->id; 
        print_r($uid);
          
        // To get user name in current logged in data
        $u_name = $u->name; 
        print_r($userName);
          
        // To get user email in current logged in data
        $u_email = $u->email; 
        print_r($u_email);

        // To get user created date in current logged in data
        $u_created = $u->created_at; 
        print_r($u_created);
    }
}

Using Helper in View

We also can use auth helper class in view (blade file) as well.

<!DOCTYPE html>
<html>
  <head>
    <title>Hello, World!</title>
  </head>
  <body>
      <h3> User Name: {{ auth()->user()->name }} </h3>
      <p> User ID: {{ auth()->user()->id }} </p>
      <p> User Email: {{ auth()->user()->email }} </p>
  </body>
</html>

Using Facade in View

We also can use facade class in view (blade file) as well.

<!DOCTYPE html>
<html>
  <head>
    <title>Hello, World!</title>
  </head>
  <body>
      <h3> User Name: {{ Auth::user()->name }} </h3>
      <p> User ID: {{ Auth::user()->id }} </p>
      <p> User Email: {{ Auth::user()->email }} </p>
  </body>
</html>

Hope this short article would help you. Have a nice day!!! 

You might also like...

Laravel  PHP 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him