Last active
September 9, 2019 20:00
-
-
Save ImtiazEpu/6287edf27916f03e0095e6fa8c55c229 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function processOrder() | |
{ | |
$validator = Validator::make(request()->all(),[ | |
'customer_name' => 'required', | |
'customer_phone_number' => 'required', | |
'address' => 'required', | |
'city' => 'required', | |
'postal_code' => 'required', | |
]); | |
if ($validator->fails()){ | |
return redirect()->back()->withErrors($validator); | |
} | |
$cart = session()->has('cart') ? session()->get('cart') : []; | |
$total = array_sum(array_column($cart, 'total_price')); | |
$order = Order::create([ | |
'user_id' => auth()->user()->id, | |
'customer_name' => request()->input('customer_name'), | |
'customer_phone_number' => request()->input('customer_phone_number'), | |
'address' => request()->input('address'), | |
'city' => request()->input('city'), | |
'postal_code' => request()->input('postal_code'), | |
'total_amount' => $total, | |
'paid_amount' => $total, | |
'payment_details' => 'Cash on delivery', | |
]); | |
foreach ($cart as $product_id => $product) { | |
$order->products()->create([ | |
'product_id' => $product_id, | |
'quantity' => $product['quantity'], | |
'price' => $product['total_price'], | |
]); | |
} | |
auth()->user()->notify(new OrderEmailNotification($order, auth()->user())); | |
session()->forget(['total', 'cart']); | |
//$this->setSuccess('Order Placed Successfully'); | |
alert()->success('Confirm', 'Order Placed Successfully')->toToast(); | |
return redirect()->route('order.details', $order->id); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment