How to get current user data only in laravel?

Member

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

How to get current user data only in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by raphael_tillman , 10 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.

Related Threads:

How to get current user id in constructor in laravel?
How to get the id of the current user in symfony?
How to check current user in laravel?
How to get the token of current user when logged in vuex?
How to get the current route name in Laravel?
How to get the id of current saved model in laravel?