首页 > 解决方案 > 表单提交返回输入 Laravel

问题描述

我正在尝试创建一个提交表单,如果用户提交表单,它应该将他重定向到确认控制器中的下一页。到目前为止,它使用这样的输入重定向回来 {"shipping_city":"gfg","shipping_phone":"087484","shipping_name":"Hellle",}

这是我的代码

结帐控制器

 public function store(Request $request)
 {
    foreach(session('cart')  as $productId =>$item);
    $product = product::find($productId);
    //Insert into orders table
    $order = Order::create([
        'shipping_city' => $request->city,
        'shipping_phone' => $request->phone,
         'shipping_name' => $request->name,
    ]);


    if ($order) {
        foreach(session('cart')  as $productId =>$item) {
           if (empty($item)) {
               continue;
           }
           $product = product::find($productId);
           OrderProduct::create([
            'order_id' => $order->id ?? null,
            'product_id' => $productId,
            'quantity' => $item['quantity'],
        ]);
       }
       return $order;
    }
    $cart = session()->remove('cart');
     return redirect()->route('confirmation.index');
   }

Checkout.blade

    <form action="{{ route('checkout.store') }}" method="POST" id="payment-form">
        {{ csrf_field() }}
         <div class=shippingform>
        <div class="form-group">
        </div>
        <div class="form-group">
            <label for="name">Name</label>
            <input type="text" class="form-control" id="name" name="name" value="{{ auth()->user()->name }}" required>
        </div>

        <div class="half-form">
            <div class="form-group">
                <label for="city">City</label>
                <input type="text" class="form-control" id="city" name="city" value="{{ old('city') }}" required>
            </div>

        </div> <!-- end half-form -->
            <div class="form-group">
                <label for="phone">Phone</label>
                <input type="text" class="form-control" id="phone" name="phone" value="{{ old('phone') }}" required>
            </div>
        <div class="spacer"></div>
        <div class="spacer"></div>
        <button type="submit" id="complete-order" class="buttons-primary full-width">Complete Order</button>
    </form>

确认控制器

  public function index()
 {
    {
        if (! session()->has('success_message')) {
            return redirect('/');
        }

        return view('thankyou');
    }
 }

路线

 Route::post('/checkout', 'CheckoutController@store')->name('checkout.store');

任何帮助将不胜感激。

标签: phplaravelformslaravel-5

解决方案


让我们看一下您的代码当前做了什么以及您希望它做什么:

带有注释的当前代码:

public function store(Request $request) {
    foreach(session('cart')  as $productId =>$item);
    $product = product::find($productId);
    //Insert into orders table
    $order = Order::create([
        'shipping_city' => $request->city,
        'shipping_phone' => $request->phone,
        'shipping_name' => $request->name,
    ]);

    if ($order) { // If a new order was successfully created
        foreach(session('cart')  as $productId =>$item) {
           // Do some stuff with each item / product
        }
        return $order; // return the newly created order with it's data
    }
    // If we didn't created a new order for whatever reason
    $cart = session()->remove('cart');
    return redirect()->route('confirmation.index'); // Redirect user to the "confirmation.index" route
}

如您所见,您只需返回创建订单的数据(当 $order 不为 false 时),否则您将重定向到“confirmation.index”页面。由于这总是与它应该做的相反(返回订单,尽管您应该被重定向到确认页面,反之亦然),您必须交换案例:

public function store(Request $request) {
    ...
    if ($order) { // If a new order was successfully created
        foreach(session('cart')  as $productId =>$item) {
           // Do some stuff with each item / product
        }
        $cart = session()->remove('cart');
        return redirect()->route('confirmation.index'); // Redirect user to the "confirmation.index" route
    }
    // If we didn't created a new order for whatever reason
    return false; // return something else, since we didn't create an order
}

推荐阅读