joh
joh

Reputation: 238

Form submission returns inputs Laravel

I'm trying to create a submission form, if a user submit the form it should redirect him to the next page which is in confirmation controller. So far it redirects back with the inputs like this {"shipping_city":"gfg","shipping_phone":"087484","shipping_name":"Hellle",}

Here is my code

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');
    }
 }

Routes

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

Any help will be appreciated.

Upvotes: 0

Views: 90

Answers (1)

Tobias F.
Tobias F.

Reputation: 1048

Let's take a look at what your code currently does and what you want it to do:

Current Code with comments:

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
}

As you can see, you just return the data of the created order (when $order is not false), otherwise you redirect to the "confirmation.index" page. Since this always does the opposite of what it should do (returning the order although you should be redirected to the confirmation page and vice versa), you have to swap the cases:

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
}

Upvotes: 1

Related Questions