Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ken4ward's avatar

Querying a single user from Lumen API

Kindly help me on how to write the api route on querying a single user. I have the one that returns all the records in the database.

This is the route

$api->group([ 'namespace' => 'App\Http\Controllers', 'middleware' => ['auth:api', 'cors'] ], function ($api)
    {
        $api->get( 'users', 'UserController@showOne', );
    });

This is the controller

public function showOne( $id )
    {
       $user = $this->userRepository->getById( $id );
       $response = $this->response->item( $user, new Transformer() );
       return $response;
    }
0 likes
3 replies
dele's avatar

I am quite confused with your code.

what is $this->response?

Vilfago's avatar
$api->get( 'users/{id}', 'UserController@showOne');

But then, you should have policy, or everyone could see every user.

You can keep your current controller, even if like @dele , I don't understand some part of it.

Nee's avatar

In this case, the id will be passed to your showOne function from the route.


$router->get('user/{id}', 'UsersController@showOne');

Please or to participate in this conversation.