在 DB::transaction() 中返回 redirect->back()

我试图在DB::transaction方法中执行一些语句,但是当某个条件失败时应该出现问题return redirect()->back()


但问题是代码既没有重定向回来也没有进一步执行。可能的原因是什么?


  return DB::transaction(function ()  use ($request){ 

            $sellerItem = new SellerItem();

            $sellerItem->item_id = $request->item_id;

            $sellerItem->unit_price = $request->unit_price;

            $sellerItem->quantity = $request->quantity;

            $sellerItem->paid_amount = $request->paid_amount; 


            $sellerItem->payment_method = $request->payment_method;

            $sellerItem->seller_id = $request->seller_id;


            $sellerItem->due = ($request->unit_price*$request->quantity) + $request->due - $sellerItem->paid_amount; 

        //   dd($sellerItem);

            $sellerItem->before_due = $request->due;

 **Problem is in this block of statement**

            if($sellerItem->due<0)

            {


            return redirect()->back()->with('success', "Paid amount can't be greater than ".abs(($request->unit_price*$request->quantity) + $request->due)); 

            }


            $sellerItem->save();

            $item = Item::find($request->item_id);

            $item->quantity = $item->quantity+$request->quantity;

            $item->save();


            return redirect('item/stock-in');


              });


子衿沉夜
浏览 271回答 1
1回答

largeQ

解决这个问题的一种方法是使用 Facade 而不是闭包。例如,使用您的代码:DB::beginTransaction();$sellerItem = new SellerItem();$sellerItem->item_id = $request->item_id;$sellerItem->unit_price = $request->unit_price;$sellerItem->quantity = $request->quantity;$sellerItem->paid_amount = $request->paid_amount;$sellerItem->payment_method = $request->payment_method;$sellerItem->seller_id = $request->seller_id;$sellerItem->due = ($request->unit_price * $request->quantity) + $request->due - $sellerItem->paid_amount;$sellerItem->before_due = $request->due;if ($sellerItem->due < 0) {&nbsp; &nbsp; DB::rollBack();&nbsp; &nbsp; return redirect()->back()->with('success',&nbsp; &nbsp; &nbsp; &nbsp; "Paid amount can't be greater than " . abs(($request->unit_price * $request->quantity) + $request->due));}$sellerItem->save();$item = Item::find($request->item_id);$item->quantity = $item->quantity + $request->quantity;$item->save();DB::commit();return redirect('item/stock-in');
打开App,查看更多内容
随时随地看视频慕课网APP