How to get current user data only in laravel?

Member

by jasen , in category: Third Party Scripts , 5 months ago

How to get current user data only in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by raphael_tillman , 4 months ago

@jasen 

To get the current user data in Laravel, you can use the Auth facade provided by Laravel.


Here is an example of how you can get the current user data:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
use IlluminateSupportFacadesAuth;

$user = Auth::user();

if ($user) {
    // User is logged in, you can access user data
    $userName = $user->name;
    $userEmail = $user->email;
    // You can access other user properties as well
} else {
    // User is not logged in
}


By calling Auth::user(), you will get the currently authenticated user if there is one, otherwise it will return null. You can then access the user's properties and use them as needed in your application.