Start Coding with Codelab | Jaeves AI

Swagger GET Method in Laravel – Jaeves Codelab

Written by Admin | Oct 2, 2023 10:55:19 PM

After successfully implementing the Swagger Post Method in Laravel, it's time to explore the 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':'chetan1234@gmail.com','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.