我有商品和购买商品。如果我购买了100件商品并卖出了50件商品,那么在此库存视图中应该显示50件商品。
在这段代码中,我只得到最后一件商品的库存。
我已附上工作图像。如何按每个商品名称获取库存?
刀片
<div class="card-content collapse show">
<div class="table-responsive">
<table
class="table table table-striped table-bordered base-style"
id="myTable"
>
<thead class="table table-striped">
<tr>
<th>Item Name</th>
<th>Stock</th>
</tr>
</thead>
<tbody>
@foreach($items as $item)
<tr>
<td>{{$item->item_name}}</td>
<td>{{$allInventory}}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
控制器
public function inventoryView()
{
$items = AllItems::where('status', 1)->get(['id','item_name']);
for ($i = 0; $i < count($items); $i++) {
$checkQuantity = Purchase::leftJoin('all_items','all_items.id', '=','purchases.item_id')
->where('purchases.item_id', $items[$i]->id)
->sum('quantity');
$checkSoldQuantity = SaleInvoiceDetail::leftJoin('all_items', 'all_items.id', '=', 'sale_invoice_details.item_id')
->where('sale_invoice_details.item_id', $items[$i]->id)
->sum('quantity');
$allInventory = round($checkQuantity - $checkSoldQuantity);
}
return view('pages.inventory-view')
->with([
'allInventory' => $allInventory,
'items' => $items,
'checkQuantity' => $checkQuantity,
'checkSoldQuantity' => $checkSoldQuantity
]);
}
答案 0 :(得分:0)
问题在这里
$allInventory = round($checkQuantity - $checkSoldQuantity);
请替换您的代码并删除变量($ allInventory,
$items[$i]['inventory'] = round($checkQuantity - $checkSoldQuantity);
在视图文件中,更改for循环
@foreach($items as $item)
<tr>
<td>{{$item->item_name}}</td>
<td>{{$item->inventory}}</td>
</tr>
@endforeach