Search code examples
laravellaravel-passport

Get Login user details in laravel resource


I am using laravel framework. I am using laravel passport for authentication. I have to check whether the user is logged in or not and then add favorite field if the user has liked that particular product. I have binded the data using resources. I am unable to find the user who is logged in since Auth::id() always returns false in resource. Is there any way to access Auth::id() in resource too?


Solution

  • See Laravel documentation https://laravel.com/docs/5.5/authentication

    // Get the currently authenticated user...
    $user = Auth::user();
    
    // Get the currently authenticated user's ID...
    $id = Auth::id();