我是新来的。这对你们来说可能很简单。按名称有3个表[账单,账单产品,产品服务]创建账单时,将数量添加到账单产品和产品服务中。更新帐单时,帐单产品和产品服务的数量增加或减少。但是当我销毁一张票据时,数量只是减去票据产品而不是产品服务
public function update(Request $request, Bill $bill)
{
$products = $request->items;
for ($i = 0; $i < count($products); $i++) {
$billProduct = BillProduct::find($products[$i]['id']);
if ($billProduct == null) {
$billProduct = new BillProduct();
$billProduct->bill_id = $bill->id;
Utility::total_quantity('minus',$products[$i]['quantity'],$products[$i]['item']);
}
else{
Utility::total_quantity('minus',$billProduct->quantity,$billProduct->product_id);
}
public function destroy(Bill $bill)
{
if (Auth::user()->can('delete bill')) {
if ($bill->created_by == Auth::user()->creatorId()) {
$bill->delete();
if ($bill->vender_id != 0) {
Utility::userBalance('vendor', $bill->vender_id, $bill->getTotal(), 'debit');
}
BillProduct::where('bill_id', '=', $bill->id)->delete();
帮助我销毁方法减去生产服务
谢谢
将此关系添加到您的Bill
模型:
public function productServices()
{
return $this->hasManyThrough(ProductService::class, BillProduct::class, 'bill_id', 'id', 'id', 'product_service_id');
}
然后使用关系来减去数量:
$productServices = $bill->productServices;
foreach ($productServices as $productService) {
$productService->quantity -= $productService->quantity;
$productService->save();
}