diff --git a/app/Http/Controllers/MainController.php b/app/Http/Controllers/MainController.php index 12b584c..d779cc8 100644 --- a/app/Http/Controllers/MainController.php +++ b/app/Http/Controllers/MainController.php @@ -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'); } diff --git a/app/Models/Order.php b/app/Models/Order.php new file mode 100644 index 0000000..70b5c62 --- /dev/null +++ b/app/Models/Order.php @@ -0,0 +1,11 @@ +id(); + $table->integer('customerId'); + $table->float('bill'); + $table->string('address'); + $table->string('name'); + $table->string('phone'); + $table->string('status'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('orders'); + } +}; diff --git a/database/migrations/2024_07_07_064427_create_order_items_table.php b/database/migrations/2024_07_07_064427_create_order_items_table.php new file mode 100644 index 0000000..8df0195 --- /dev/null +++ b/database/migrations/2024_07_07_064427_create_order_items_table.php @@ -0,0 +1,31 @@ +id(); + $table->integer('productId')->nullable(); + $table->integer('quantity')->nullable(); + $table->float('price')->nullable(); + $table->integer('orderId')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('order_items'); + } +}; diff --git a/resources/views/cart.blade.php b/resources/views/cart.blade.php index 27e5a4d..bdafe33 100644 --- a/resources/views/cart.blade.php +++ b/resources/views/cart.blade.php @@ -27,11 +27,19 @@
@@ -91,13 +106,19 @@ - + | Nrs. {{ $item->price * $item->quantity }} |
-
+ + + |