Start Coding with Codelab | Jaeves AI

Swagger Post Method in Laravel – Jaeves Codelab

Written by Admin | Oct 2, 2023 10:57:12 PM

In previous two articles we Create Laravel Swagger API and Create Swagger Configuration in Laravel. Now we will add Swagger Post Method in Laravel.


     /**
     * @OA\Post(
     *      path="/update-user/{id}",
     *      operationId="updatePassword",
     *      tags={"User"},
     *      summary="Update Password",
     *      description="Update Password",
     *      @OA\RequestBody(
     *      required=true,
     *      description="Update Password parameters",
     *		@OA\Parameter(
     *      name="id",
     *      in="path",
     *      required=true,
     *      description= "user id",
     *      example="10",
     *      @OA\Schema(
     *           type="integer"
     *      )
     *      ),
     *      @OA\MediaType(
     *            mediaType="multipart/form-data",
     *            @OA\Schema(
     *               type="object",
     *       @OA\Property(property="full_name", type="string", format="string", example="Chetan", description ="Your full name"),
     *       @OA\Property(property="last_name", type="string", format="string")
     *            )
     *        ),
     *      ),
     *       @OA\Response(
     *      response=200,
     *      description="Success response",
     *      @OA\JsonContent(
     *      @OA\Property(property="status", type="number", example="200"),
     *      @OA\Property(property="message", type="string", example="User updated successfully."),
     *        )
     *     ),
     *        @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 updateUser($id,Request $request)
    {
    	try {
	    	$user = User::where('id', $id)->update(['full_name' => $request->input('full_name')]);

	    	if($user){
	    		return response()->json(['status' => 200, 'message' => 'User updated successfully'], 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.

Check Laravel swagger GET Method.

Read laravel swagger documentation here.