Laravel无法使用UpdateExistingPivot方法

时间:2019-08-31 05:52:01

标签: php mysql laravel lumen

我正在尝试更新数据透视表中的值。这是我的方法:

$order = OrderDetails::where('id', $orderId)->first();

$products = $order->products()->get();
// gives items from pivot table in form of array of objects having productId,pivot details
foreach ($products as $key => $value) {
    if ($value) {
        $item = Items::where('productId', $value->productId)->first();
        if ($item->stock > 0) {

            if ($item->stock >= $value->pivot->quantity) {
                $item->stock = $item->stock - $value->pivot->quantity;
                $order->products()
                    ->updateExistingPivot($item->productId,[
                        'quantity' => $value->pivot->quantity,
                        'shelfLife' => $value->pivot->shelfLife,
                        'available' => '1'
                    ]);
            } else {
                $order->products()->updateExistingPivot($item->productId, [
                    'quantity' => $value->pivot->quantity,
                    'shelfLife' => $value->pivot->shelfLife,
                    'available' => '0'
                ]);
            }
            $item->updatedAt = Carbon::now();
            if ($item->stock <= 3) {
                $item->active = 0;
            }
            if ($item->save()) {
                //nothing
            } else {
                return response()->json('Some Error Occured');
            }
        } else {
            $order->products()->updateExistingPivot($item->productId, [
                'quantity' => $value->pivot->quantity,
                'shelfLife' => $value->pivot->shelfLife,
                'available' => '0'
            ]);
        }
    }
}

错误

  

找不到列:1054'字段列表'中的未知列'0'(SQL:更新orders设置updated_at = 2019-08-31 05:35:54,0 = {\“ productId \”:\“ GRO-PUL-MAS-RMD1-42760715 \”,\“ qty \”:\“ 1 \”,\“ pivot \”:{\“ orderId \”:\“ ODDSMhragn190831736922765 \“,\” productId \“:\” GRO-PUL-MAS-RMD1-42760715 \“,\”数量\“:\” 1 \“,\” shelfLife \“:\” 12个月\“,\”可用\“:\” 1 \“}},其中id =?)位于/storage/ssd4/483/8865483/vendor/illuminate/database/Connection.php:664,PDOException(code:42S22)

我不知道为什么updateExistingPivot()方法不起作用。希望您能提供帮助。

0 个答案:

没有答案