Swagger GET Method in Laravel

Code Examples, Laravel

In previous article we Create Swagger Post Method in Laravel. Now we will create Swagger GET Method in Laravel.

/**
     * @OA\Get(
     *      path="/get-user/{id}",
     *      operationId="getUser",
     *      tags={"User"},
     *      summary="Get User",
     *      description="Get User",
     *      @OA\Parameter(
     *      name="id",
     *      in="path",
     *      required=true,
     *      description= "user id",
     *      example="10",
     *      @OA\Schema(
     *           type="integer"
     *      )
     *      ),
     *       @OA\Response(
     *      response=200,
     *      description="Success response",
     *      @OA\JsonContent(
     *      @OA\Property(property="status", type="number", example="200"),
     *      @OA\Property(property="user", type="string", example="{'full_name':'Chetan','email_id':'[email protected]','created_at':'2022-05-27T07:16:57.000000Z','updated_at':'2022-05-27T07:16:57.000000Z'}"),
     *        )
     *     ),
     *        @OA\Response(
     *      response=400,
     *      description="Bad Request",
     *      @OA\JsonContent(
     *      @OA\Property(property="status", type="number", example="400"),
     *      @OA\Property(property="message", type="string", example="Error in processing request")
     *        )
     *     )
     * )
     *      
     * )
     */

    public function getUser($id)
    {
    	try {
	    	$user = User::where('id', $id)->get();

	    	if($user){
	    		return response()->json(['status' => 200, 'user' => $user], 200);
	    	}
    	}catch (\Exception $e) {
            return response()->json(['status' => 400, 'message' => 'Error in processing request'], 400);
        }
    }

Now, run php artisan l5-swagger:generate and check api at https://mywebsite.com/api/documentation.

Read laravel swagger documentation here.