Karthik
Karthik

Reputation: 5757

How to get User id using Passport Token in Laravel

How to get Current User Id from posted Passport Auth Token in Laravel. I use

$request->user()->id;

is correct method for get current user id in laravel API

Upvotes: 3

Views: 11657

Answers (3)

Rahul Soni
Rahul Soni

Reputation: 11

Try this code.

   use Auth;    
    class MyController extends Controller {    
       public function myFunc(){    
          dd(Auth::user()->id);    
       }    
    }

Upvotes: 1

Deepesh Thapa
Deepesh Thapa

Reputation: 1789

auth('api')->user() returns the authenticated user (or null if the user is not logged on) - even if middleware is not used.

Upvotes: 2

Aftab H.
Aftab H.

Reputation: 1537

To get user id, you can use as below: Detailed you can see Reference.

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user()->id;
});

Upvotes: 4

Related Questions