У меня есть предметы и предметы покупки. Если я куплю 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
]);
}