FlowingCode
FlowingCode

Reputation: 101

Call api resource and return current user

I want to return all data of a single user and I want to use a resource for it.

As I get it, such resources cannot be called from controllers (e.g. because of the $request parameter in the resource toArray(..) method). So, I have to call it directly from my api.php file.

If I call it from my api.php now, I do not get any $request-parameter from the parent-function (Route::get(..)), so I cannot recognize, which user calls the resource function. How do I tackle this problem?

By the way, I do not want to use something like this: Route::get('/user/{userId}', ...)

Upvotes: 1

Views: 553

Answers (3)

Waqar Yazdani
Waqar Yazdani

Reputation: 31

I hope this helps you can directly get user data by calling this route.

Route::get('user', function (Request $request) {
        return $request->user();
    });

Upvotes: 2

loic.lopez
loic.lopez

Reputation: 2103

You can grab your current logged user by:

public void user(Request $request)
 {
      $user = $request->user();
      return $user;
 }

Upvotes: 1

Jatin Parmar
Jatin Parmar

Reputation: 2900

in order to use request directly from any function you should get current request object by using request helper function inside called function

$request=request();

hope this will help

Upvotes: 2

Related Questions