如果最后一个被删除,则获取下一个 id

我有 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,谢谢。


肥皂起泡泡
浏览 87回答 2
2回答

繁星coding

如果您在变量save()上使用函数$bill,您将通过访问$bill->id. 如果您想确保您的交易完成,您可以使用DB transactions。use DB;class ... {&nbsp; &nbsp;...&nbsp; &nbsp; public function store(Request $request)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $brand_id = auth('brand')->id();&nbsp; &nbsp; &nbsp; &nbsp; $bill = new Bill();&nbsp; &nbsp; &nbsp; &nbsp; $bill->brand_id = $brand_id;&nbsp; &nbsp; &nbsp; &nbsp; $bill->date = Carbon::today();&nbsp; &nbsp; &nbsp; &nbsp; DB::beginTransaction();&nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $bill->save();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data = $request->all();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $products = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $total = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for($i = 0 ; $i<count($data['id']) ; $i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $prod = new ProductsBill();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $prod->product_id = $data['id'][$i] ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $prod->quantity = $data['quantity'][$i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $prod->bill_id = $bill->id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $products[] = $prod->attributesToArray();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $price = Product::find($prod->product_id)->price;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $total += $price * $prod->quantity;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $bill->total = $total;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $bill->save();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ProductsBill::insert($products);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DB::commit();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return redirect()->back();&nbsp; &nbsp; &nbsp; &nbsp; } catch(\Illuminate\Database\QueryException $ex){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DB::rollBack();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Exception redirect here&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; ...}

慕运维8079593

如果我正确理解您的问题,您可能可以使用 mysql 函数来获取最后插入的 ID 并添加一个。https://dev.mysql.com/doc/refman/8.0/en/information-functions.html#function_last-insert-id
打开App,查看更多内容
随时随地看视频慕课网APP