我有 bill 和 product_bill 表。
我需要在 product_bill 表中使用 bill_id 并且我应该首先创建账单,所以 id 这样做了
public function store(Request $request)
{
if( Bill::first() == null )
{
$bill_id = 1;
}
else {
$bill_id = Bill::orderBy('id','desc')->first()->id +1 ;
}
// dd($bill_id);
$brand_id = auth('brand')->id();
$bill = new Bill();
$bill->brand_id = $brand_id;
$bill->date = Carbon::today();
$data = $request->all();
$products = [];
$total = 0;
for($i = 0 ; $i<count($data['id']) ; $i++)
{
$prod = new ProductsBill();
$prod->product_id = $data['id'][$i] ;
$prod->quantity = $data['quantity'][$i];
$prod->bill_id = $bill_id;
$products[] = $prod->attributesToArray();
$price = Product::find($prod->product_id)->price;
$total += $price * $prod->quantity;
}
$bill->total = $total;
$bill->save();
ProductsBill::insert($products);
return redirect()->back();
}
当我删除任何账单时,product_bill 表中的新 bill_id 会获得最后一个的下一个数字,我需要知道下一个将创建该 ID,谢谢。
繁星coding
慕运维8079593