【问题标题】:i want to reduce products quantity after an order delivered laravel 7我想在订单交付后减少产品数量 laravel 7
【发布时间】:2020-09-05 21:29:42
【问题描述】:

这是我确认订单已送达的按钮

@elseif($order->order_status == 'Confirmed')
        <div >
          <form enctype="multipart/form-data" action="{{route('updateorderstatus',['order'=>$order->id])}}" method="post" >
           @csrf
<input  name="order_id" type="hidden" value="{{$order->id}}"/>
          <input name="order_status" type="hidden" value="Shipping"/>
              <a data-toggle="tooltip" data-placement="top" title="">
        <button  type="submit" class="btn btn-link">
        <i class="fa fa-plane" aria-hidden="true"></i>

        </button>
       </a>
     </form>
     </div>
@elseif( $order->order_status == 'Shipping'  )
                                    <div   class="btn-group" role="group" aria-label="Third group">
                                        <form enctype="multipart/form-data" action="{{route('updateorderstatus')}}" method="post" >
                                            @csrf
                                            <input  name="order_id" type="hidden" value="{{$order->id}}"/>
                                            <input name="order_status" type="hidden" value="Delivered"/>
                                            <input  name="quantity" type="hidden" value="{{$order->quantity}}"/>
                                            <input  name="product_id" type="hidden" value="{{$order->product_id}}"/>
                                            <a data-toggle="tooltip" data-placement="top" title="">
                                                <button  type="submit" class="btn btn-link">
                                                    <i class="fa fa-check-square-o" aria-hidden="true"></i>
                                                </button>
                                            </a>
                                        </form>
                                    </div>

更新订单状态和我尝试减少数量的功能

public function updateorderstatus(Request $request)

{
    $order = Order::all();
    if ($request->isMethod('post')  ){

            $data = $request->all();

            Order::where('id',$data['order_id'])->update(['order_status'=>$data['order_status']]);


             $current = Order::where('order_status','Delivered')->get();

              Dvproduct::where('product_id',$data['product_id'])->update( ['quantity'=>$current - $data['quantity']] );


    }


    return back();

}

我有 2 个错误

当我点击第一个按钮时,我得到Undefined index: product_id

当我点击第二个Object of class Illuminate\Database\Eloquent\Collection could not be converted to number

【问题讨论】:

  • 您面临的第一个错误是因为在这两种形式中您都有一个 post 方法,因此您的 if 条件在两种形式中都为真。在一种形式中你有 product_id 但在另一种形式中你没有这就是你面临这个错误的原因。
  • 所以我必须将输入 product_id 添加到其他表单
  • 更新订单状态只是通过 id 更新状态顺序,它工作正常,函数中的最后两行我只是添加它们试图减少当状态 ='delivered' 时的数量我应该做交付表格的其他功能??
  • 在哪里传递状态,product_id 与我在表单中获得的 $data['product_id'] 相同
  • 我应该在get()中添加的数量?

标签: sql laravel laravel-7


【解决方案1】:

这里稍作修正,也尝试为有序按钮制作另一个功能和路线

改变

$current = Order::where('order_status','Delivered')->get();

$current = Order::where('order_status','Delivered')->sum('quantity');

现在可以了

Dvproduct::where('product_id',$data['product_id'])->update( ['quantity'=>$current - $data['quantity']] );

【讨论】:

    猜你喜欢
    • 2017-01-28
    • 2016-08-16
    • 1970-01-01
    • 2017-11-13
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    • 2019-04-21
    • 2014-05-04
    相关资源
    最近更新 更多