Laravel 10|9|8 Get Logged in User Data(id, Name, Email, etc)

If you are working in a Laravel web application and you want to get logged-in user data like user id, name, email or any other user-related data on controller, model, and blade views. So in this Laravel tutorial, you can learn.

Through this tutorial, you will learn how to get or retrieve logged-in user data like user id, name, email, or any user data on controller, blade, & model file in laravel 10, 9, 8, and 7.

How to Get Logged-in User Data on Controller, Blade & Model in Laravel 10|9|8

Here are 4 ways to get or retrieve logged-in user data like user id, name, email, or any other user-related data blade, model & controller in laravel 10, 9, 8, 7:

  • To Get Logged In User in Controller using Helper
  • To Get Logged In User in Controller using Facade
  • To Get Logged In User in View Blade using Helper
  • To Get Logged In User in View Blade using Facade

To Get Logged In User in Controller using Helper

Using the helper, you can get logged In user data on controller in laravel:

    public function index(Request $request)
    {

        /* Current Login User Details */
        $user = auth()->user();
        var_dump($user);

        /* Current Login User ID */
        $userID = auth()->user()->id;
        var_dump($userID);

        /* Current Login User Name */
        $userName = auth()->user()->name;
        var_dump($userName);

        /* Current Login User Email */
        $userEmail = auth()->user()->email;
        var_dump($userEmail);
    }

To Get Logged In User in Controller using Facade

Using the facade, you can get logged In user data on controller in laravel:

    public function index(Request $request)
    {
        /* Current Login User Details */
        $user = Auth::user();
        var_dump($user);

        /* Current Login User ID */
        $userID = Auth::user()->id;
        var_dump($userID);

        /* Current Login User Name */
        $userName = Auth::user()->name;
        var_dump($userName);

        /* Current Login User Email */
        $userEmail = Auth::user()->email;
        var_dump($userEmail);
    }

To Get Logged In User in View Blade using Helper

Using the helper, you can get logged In user data on blade view in laravel:

<p> User ID: {{ auth()->user()->id }} </p>
<p> User Name: {{ auth()->user()->name }} </p>
<p> User Email: {{ auth()->user()->email }} </p>

To Get Logged In User in View Blade using Facade

Using the facede, you can get logged In user data on blade view in laravel:

<p> User ID: {{ Auth::user()->id }} </p>
<p> User Name: {{ Auth::user()->name }} </p>
<p> User Email: {{ Auth::user()->email }} </p>

Conclusion

Through this tutorial, you have learned how to get/retrieve logged In user data from database or session on controller and blade view file in laravel 10, 9, 8, 7.

Recommended Laravel Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *