cari

Rumah  >  Soal Jawab  >  teks badan

Apabila saya cuba mengemas kini siaran saya dengan cara yang sombong, saya mendapat ralat berikut

Ini adalah pengawal saya dan saya berlagak di sini untuk menukar

class ProductController extends Controller

public function __construct()
{
    $this->middleware('auth:api');
}


public function getdata()
{
    $products = Product::get();
    return view('table', [
        'products'=>$products
    ]);
}

Di sini saya mendapat semua data daripada pangkalan data

/**
 * @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));
}

Di sini saya menyimpan data dari swagger

/**
 * @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,

    ]);

}

Di sini saya hanya mendapatkan id elemen yang dipilih untuk paparan

/**
 * @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);
}

Apabila saya cuba menukar data sedia ada dalam pangkalan data, saya selalu mendapat ralat seperti di bawah.

/**
 * @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,
    ]);
}

Ralat ini berlaku dalam Swagger

Gagal diperolehi. sebab yang mungkin: Perkongsian sumber merentas domain kegagalan rangkaian Skim URL mestilah "http" atau "https" untuk permintaan perkongsian sumber merentas domain.

Pepijat ini dalam Posmen

"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."
    ]
}

Di sini saya memadamkan elemen daripada pangkalan data

/**
 * @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,
    ]);
}

}

P粉550823577P粉550823577280 hari yang lalu377

membalas semua(1)saya akan balas

  • P粉248602298

    P粉2486022982024-02-26 15:37:13

    Menurut ralat posmen, nampaknya ia tidak dapat mendapatkan medan (nama, taip...) daripada permintaan. dd meminta objek dan lihat apa yang berlaku.

    balas
    0
  • Batalbalas