duandingcu7010 2018-10-12 07:49
浏览 56

Laravel没有在多个请求上完成控制器方法

I tried to solve multiple clicking on the submit button by using ReCaptcha V3. It kind of works as the database insert only run once in the controller and the following times it just hit the validation returning an error.

The issue is that the controller method is not entirely run.

For example if I click multiple times on a submit button on an e-commerce page, in the CheckoutController, the checkout method doesn't run completely.

class CheckOutController extends Controller {

    public function checkout(Request $request) {
        // Some checkout Logic (insert into database)
        $this->validate($request, [
                'recaptcha' => ['required', new \App\Rules\Recaptcha]
            ]);
        if (Cart::content()->count() > 0) {
            foreach (Cart::content() as $cartItem) {
              $insert = new \App\Transaction;
              $insert->product_id = $cartItem->id;
              $insert->receipt_id = $cartItem->receipt_id;
              $insert->quantity = $cartItem->qty;
              $insert->price= $cartItem->price;
              $insert->save();
            }
         Cart::destroy(); //last part of checkout logic
        return view('finishCheckout');
        }
        return abort(404);  
    }
}

Because I'm working locally the speed is faster so the request is iterating through my code inserting the data into my database.

Sometimes the request hits the Cart:: destroy(); part of the logic, sometimes it doesn't. But I suspect that on a production environment the code might stop elsewhere before that part.

And it never hits the return view('finishCheckout');. It just retrying the same method and fails the validation, returning me back to the checkout page with the validation error.

Is there anything I can do to ensure that either all the method is run or stop it from running completely?

Edit: Note that this only happens when I click the submit buttons multiple times! If I would click it only once, the method runs correctly.

  • 写回答

3条回答 默认 最新

  • dongtu7205 2018-10-12 08:16
    关注

    Before you delete use \Cart

    You can use Cart::delete() or Cart::where(....)->delete()

    评论

报告相同问题?