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.