這是我的控制器,我在這里大搖大擺地切換
class ProductController extends Controller
public function __construct()
{
$this->middleware('auth:api');
}
public function getdata()
{
$products = Product::get();
return view('table', [
'products'=>$products
]);
}
在這里,我從資料庫中獲取所有資料
/**
* @return array<object string/int>
*
*
* @OA\Get(
* path="/api/products",
* tags={"Products"},
* summary="Get all products",
* description="For getting all datas should be pressed the button called 'try'",
* operationId="index",
* @OA\Parameter(
* name="paginate",
* in="query",
* description="Status values that needed to be considered for filter",
* required=true,
* explode=true,
* @OA\Schema(
* default="10",
* type="string",
* )
* ),
* @OA\Response(
* response=200,
* description="successful operation",
* ),
* security={{ "bearer": {} }}
* )
*/
public function index($paginate=10){
return UserResource::collection(Product::paginate($paginate));
}
在這里我存盤來自招搖的資料
/**
* @OA\Post(
* path="/api/products",
* tags={"Products"},
* summary="Create a new type of item to the product",
* operationId="store",
* @OA\RequestBody(
* description="Create a new item",
* required=true,
* @OA\MediaType(
* mediaType="multipart/form-data",
* @OA\Schema(
* type="object",
* @OA\Property(
* property="name",
* description="Give a new name to the product",
* type="string",
* ),
* @OA\Property(
* property="type",
* description="Give a new type to the product",
* type="string",
* ),
* @OA\Property(
* property="price",
* description="Give a new type to the product",
* type="string",
* ),
* @OA\Property(
* property="image",
* description="Give a new type to the product",
* type="file",
* ),
* ),
* ),
* ),
* @OA\Response(
* response=200,
* description="successful operation",
* ),
* security={{ "bearer": {} }}
* )
*/
public function store(ProductRequest $request)
{
$request->validated();
if ($request->hasFile('image')) {
$path = $request->file('image')->store('images', 'public');
$product = new Product;
$product->image = $path;
$product->name = $request->name;
$product->type = $request->type;
$product->price = $request->price;
$product->save();
}
return response()->json([
'status' => 'success',
'message' => 'Product created successfully',
'products' => $product,
]);
}
在這里,我只是獲取 id 選擇的元素以便顯示
/**
* @OA\Get(
* path="/api/product/{id}",
* tags={"Products"},
* summary="Show the choosen element",
* operationId="show",
* @OA\Parameter(
* name="id",
* in="path",
* description="Status values that needed to be considered for filter",
* required=true,
* explode=true,
* @OA\Schema(
* default="1",
* type="string",
* )
* ),
* @OA\Response(
* response=200,
* description="successful operation",
* ),
* security={{ "bearer": {} }}
* )
*
*/
public function show($id)
{
return UserResource::collection(Product::all()->keyBy->id);
}
當我嘗試更改資料庫中的現有資料時出現錯誤,我總是得到下面顯示的錯誤。
/**
* @OA\Put(
* path="/api/product/{id}/update",
* tags={"Products"},
* summary="Update the choosen element",
* operationId="update",
* @OA\Parameter(
* name="id",
* in="path",
* description="Status values that needed to be considered for filter",
* required=true,
* @OA\Schema(
* type="string",
* )
* ),
* @OA\RequestBody(
* description="Update a new item",
* required=true,
* @OA\MediaType(
* mediaType="multipart/form-data",
* @OA\Schema(
* type="object",
* @OA\Property(
* property="name",
* description="Update",
* type="string",
* ),
* @OA\Property(
* property="type",
* description="Update",
* type="string",
* ),
* @OA\Property(
* property="price",
* description="Update",
* type="string",
* ),
* @OA\Property(
* property="image",
* description="Update",
* type="file",
* ),
* ),
* ),
* ),
* @OA\Response(
* response=200,
* description="successful operation",
* ),
* @OA\Response(
* response=400,
* description="Invalid user supplied"
* ),
* @OA\Response(
* response=404,
* description="User not found"
* ),
* security={{ "bearer": {} }}
* )
*
*/
public function update(ProductRequest $request, $id){
/* Gate::authorize('update', $product); */
$product = Product::find($id);
if ($request->hasFile('image')) {
if (isset($product->image)) {
Storage::delete($product->image);
}
$path = $request->file('image')->store('images', 'public');
}
$product->update([
"name" => $request->name,
"type" => $request->type,
"price" => $request->price,
"image" => $path ?? $product->image
]);
return response()->json([
'status' => 'success',
'message' => 'The choosen product updated successfully',
'product' => $product,
]);
}
大搖大擺的這個錯誤
獲取失敗。可能的原因: CORS 網路故障 URL 方案對于 CORS 請求必須是“http”或“https”。
郵遞員的這個錯誤
"message": "The given data was invalid.",
"errors": {
"name": [
"The name field is required."
],
"type": [
"The type field is required."
],
"price": [
"The price field is required."
],
"image": [
"The image field is required."
]
}
在這里我從資料庫中洗掉元素
/**
* @OA\Delete(
* path="/api/product/{id}",
* tags={"Products"},
* summary="Show the choosen element",
* operationId="destroy",
* @OA\Parameter(
* name="id",
* in="path",
* description="Status values that needed to be considered for filter",
* required=true,
* explode=true,
* @OA\Schema(
* default="1",
* type="string",
* )
* ),
* @OA\Response(
* response=200,
* description="successful operation",
* ),
* security={{ "bearer": {} }}
* )
*
*/
public function destroy($id)
{
$product = Product::find($id);
$product->delete();
return response()->json([
'status' => 'success',
'message' => 'product deleted successfully',
'product' => $product,
]);
}
}
uj5u.com熱心網友回復:
根據郵遞員錯誤,它看起來無法從請求中獲取欄位(名稱,型別...)。dd 請求物件,看看會發生什么。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/517413.html
下一篇:C語言簡易計算器
