我有一个要更新到库存表的数据数组列表。
$data['items'] = DB::table('inventory_items')->select('inventory_items.*')
->join('sorder_parts', 'inventory_items.id', 'sorder_parts.inventory_id')
->where('sorder_parts.sorder_id', '=', $id)
->selectraw('inventory_items.quantity - sorder_parts.quantity AS new_quantity')
->get()->toArray();
foreach ($data as $product_item) {
$reduce_quantity = InventoryItem::updateOrCreate(['id' => $product_item['items']['id']],
['quantity' => $product_item['quantity']['new_quantity']]);
}
可能 $product_item 变量没有 items 键。尝试将“$product_item['items']['id']”更改为“$product_item['id']”。
因为带有 get() 方法的“DB”门面不会返回深层数组。