app/Http/Controllers/ProductsController.php
/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\products  $products
     * @return \Illuminate\Http\Response
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(Request $request, products $products)
    {
        $this->validate($request, [
            'productId' => 'required|int',
            'name' => 'required|string',
            'description' => 'required|string',             
            'quantity' =>  'required|string',
            'price' => 'required|string',
            'farm_id'  => 'required|string',
            'category_id'  => 'required|string',
            'measurement_id' => 'required|string',          
          ]);
          $productToUpdate = products::find($request->productId);
          $productToUpdate->update([
            'name' => $request->name,
            'description' => $request->description,
            'quantity '=>  $request->quantity,
            'price' =>  $request->price,
            'farm_id' => $request->farm_id,
            'category_id' =>  $request->category_id,
            'measurement_id' => $request->measurement_id
          ]);
        if($productToUpdate->errors ||  empty($productToUpdate)){
            return response()->json( ['message'=>' Cannot update the product. Either the id provided does not exist.'], 424);
        }
        return  response()->json( $productToUpdate, 201);
    }
public function update ( Request $ request , products $ products ) { $ this -> validate ( $ request , [ 'productId' => 'required|int' , 'name' => 'required|string' , 'description' => 'required|string' , 'quantity' => 'required|string' , 'price' => 'required|string' , 'farm_id' => 'required|string' , 'category_id' => 'required|string' , 'measurement_id' => 'required|string' , ] ) ; $ productToUpdate = products :: find ( $ request -> productId ) ; $ productToUpdate -> update ( [ 'name' => $ request -> name , 'description' => $ request -> description , 'quantity ' => $ request -> quantity , 'price' => $ request -> price , 'farm_id' => $ request -> farm_id , 'category_id' => $ request -> category_id , 'measurement_id' => $ request -> measurement_id ] ) ; if ( $ productToUpdate -> errors || empty ( $ productToUpdate ) ) { return response ( ) -> json ( [ 'message' => ' Cannot update the product. Either the id provided does not exist.' ] , 424 ) ; } return response ( ) -> json ( $ productToUpdate , 201 ) ; }
Update the specified resource in storage.
Update the specified resource in storage .
// retun error message