Place Order
This commit is contained in:
@ -8,6 +8,8 @@ use Illuminate\Support\Facades\DB;
|
||||
|
||||
use App\Models\Cart;
|
||||
use App\Models\Products;
|
||||
use App\Models\Order;
|
||||
use App\Models\OrderItem;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
@ -163,25 +165,14 @@ class MainController extends Controller
|
||||
return redirect()->back()->with('success', 'Item deleted from cart successfully!');
|
||||
}
|
||||
|
||||
// public function updateCartItem(Request $data, $id)
|
||||
// {
|
||||
// if (session()->has('id')) {
|
||||
// $item = Cart::find($data->input('id'));
|
||||
// $item->quantity = $data->input('quantity');
|
||||
// $item->save();
|
||||
// return redirect()->back()->with('success', 'Item updated successfully!');
|
||||
// } else {
|
||||
// return redirect('/login')->with('error', 'Please login to update item!');
|
||||
// }
|
||||
// }
|
||||
|
||||
public function updateCartItem(Request $request, $id)
|
||||
public function updateCartItem(Request $data, $id)
|
||||
{
|
||||
// dd($request->all());
|
||||
if (session()->has('id')) {
|
||||
$item = Cart::find($id);
|
||||
if ($item) {
|
||||
$item->quantity = $request->input('quantity');
|
||||
$item->quantity = $data->input('quantity');
|
||||
$item->save();
|
||||
return redirect()->back()->with('success', 'Item updated successfully!');
|
||||
} else {
|
||||
@ -201,8 +192,36 @@ class MainController extends Controller
|
||||
|
||||
|
||||
|
||||
public function checkout()
|
||||
public function checkout(Request $data)
|
||||
{
|
||||
if (session()->has('id')) {
|
||||
$order = new Order();
|
||||
$order->status = "Pending";
|
||||
$order->customerId = session()->get('id');
|
||||
$order->name = $data->input('name');
|
||||
$order->phone = $data->input('phone');
|
||||
$order->address = $data->input('address');
|
||||
$order->bill = $data->input('bill');
|
||||
if ($order->save()) {
|
||||
$cartItems = Cart::where('customerId', session()->get('id'))->get();
|
||||
foreach ($cartItems as $item) {
|
||||
$product = Products::find($item->productId);
|
||||
$orderItem = new OrderItem();
|
||||
$orderItem->orderId = $order->id;
|
||||
$orderItem->productId = $item->productId;
|
||||
$orderItem->quantity = $item->quantity;
|
||||
$orderItem->price = $product->price;
|
||||
|
||||
$orderItem->save();
|
||||
$item->delete();
|
||||
}
|
||||
return redirect()->back()->with('success', 'Order placed successfully!');
|
||||
} else {
|
||||
return redirect('login')->back()->with('error', 'Order not placed!');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return view('checkout');
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user