我正在使用Laravel 7开发一个api,并且使用Swagger编写API文档。我的问题是我想使用Swagger API上传图像,但是它不只在打印图像时显示[object File],所以我不能将它们上传到公用文件夹。我的商店方法
/**
* Store a newly created Products.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*
* @OA\Post(
* path="/api/products",
* tags={"Product"},
* @OA\Response(response="200", description="Adding a new Product."),
*
* @OA\Parameter(
* name="name[]",
* in="query",
* description="Product Name",
* required=true,
* @OA\Schema(type="array", @OA\Items(type="string")),
* ),
* @OA\Parameter(
* name="images[]",
* in="query",
* description="Product Images",
* required=true,
* @OA\Schema(type="array", @OA\Items(type="file")),
* ),
* * )
* )
*
*/
public function upload(Request $request)
{
$request->validate([
"name" => "required",
"images" => "required",
]);
$images = $request->images;
$product = Product::create([]);
if(! is_dir(public_path("/products")) ){
mkdir(public_path("/products"), 0777);
}
$id = $product->id;
$imageCollection = Collection::wrap($images);
$imageCollection->each(function($image) use ($id){
$basename = Str::random();
$original = $basename . "." . $image->getClientOriginalExtension();
$image->move(public_path("/products"), $original);
ProductMedia::create([
"product_id" => $id,
"image" => "/products/".$original,
]);
});
return Product::where("id", $product->id)->with("productMedia")->get();
}
如何通过Swagger API添加图像?