表单提交返回输入Laravel

时间:2019-06-21 11:02:27

标签: php laravel forms laravel-5

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

这是我的代码

CheckoutController

 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>

ConfirmationController

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

        return view('thankyou');
    }
 }

路线

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

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

让我们看看您的代码当前正在做什么以及您希望它做什么:

带有注释的当前代码:

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
}