abhishekregmi's avatar

post method working but put and patch not working in laravel swagger api

when i use put/patch where mediatype="application/json" it works finely but i need to upload file too. how can i fix this ?

/**
     * create testimonial 
     * @OA\put(
     *     path="/api/testimonial/store",
     *     tags={"testimonial"},
     * security={{"apiAuth":{}}},
     *     @OA\RequestBody(
     *         required=true,
     *         @OA\MediaType(
     *             mediaType="multipart/form-data",
     *             @OA\Schema(
     *                 @OA\Property(property="name",type="string"),
     *                 @OA\Property(property="description",type="string"),
     *                 @OA\Property(property="designation",type="string"),
     *                 @OA\Property(property="company",type="string"),
     *                 @OA\Property(property="image",type="file",format="string"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="successful operation",
     *     ),
     *  @OA\Response(
     *          response=400,
     *          description="invalid",
     *          @OA\JsonContent(
     *              @OA\Property(property="msg", type="string", example="fail"),
     *          )
     *      )

     * )
     * */
    public function store(Request $request)
    {
        try {
            $testimonial = new Testimonial;
            $testimonial->name = $request->name;
            $testimonial->description = $request->description;
            $testimonial->designation = $request->designation;
            $testimonial->company = $request->company;
            if ($request->hasfile('image')) {
                $file = $request->file('image');
                $extenstion = $file->getClientOriginalExtension();
                $filename = time() . '.' . $extenstion;
                $file->move('uploads/testimonial/', $filename);
                $testimonial->image = $filename;
            }
            $testimonial->save();
            return $this->returnJson($testimonial, 'Testimonials successfully created!', 200, ['total' => $testimonial->count()]);
        } catch (Exception $e) {
            return $this->returnJson(false, $e->getMessage(), $e->getCode());
        }
    }
0 likes
1 reply

Please or to participate in this conversation.