From 49d6315cc4eb58390b1645924af7b9f6c037bc84 Mon Sep 17 00:00:00 2001 From: UronShrestha Date: Sun, 7 Jul 2024 13:06:31 +0545 Subject: [PATCH] Place Order --- app/Http/Controllers/MainController.php | 47 +++++++++++------ app/Models/Order.php | 11 ++++ app/Models/OrderItem.php | 11 ++++ .../2024_07_07_064420_create_orders_table.php | 33 ++++++++++++ ..._07_07_064427_create_order_items_table.php | 31 ++++++++++++ resources/views/cart.blade.php | 50 ++++++++++++++++--- resources/views/components/footer.blade.php | 19 +++++++ resources/views/components/header.blade.php | 12 ++--- routes/web.php | 2 +- 9 files changed, 189 insertions(+), 27 deletions(-) create mode 100644 app/Models/Order.php create mode 100644 app/Models/OrderItem.php create mode 100644 database/migrations/2024_07_07_064420_create_orders_table.php create mode 100644 database/migrations/2024_07_07_064427_create_order_items_table.php 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 @@
@if (session()->has('success')) -
+
{{ session()->get('success') }}
@endif + @if (session()->has('error')) +
+ {{ session()->get('error') }} +
+ @endif + + + @@ -72,7 +80,14 @@ @endforeach --}} + + + @php + $total = 0; + @endphp + + @foreach ($cartItems as $item) - + + @php + $total += $item->price * $item->quantity; + @endphp @endforeach @@ -129,10 +150,27 @@
Cart total
    -
  • Subtotal $ 169.50
  • -
  • Total $ 169.50
  • +
  • Subtotal Nrs {{ $total }}
  • +
  • Total Nrs {{ $total }}
- Proceed to checkout +
+ + + + + + + + + +
diff --git a/resources/views/components/footer.blade.php b/resources/views/components/footer.blade.php index b036c51..67f176b 100644 --- a/resources/views/components/footer.blade.php +++ b/resources/views/components/footer.blade.php @@ -90,4 +90,23 @@ + + + diff --git a/resources/views/components/header.blade.php b/resources/views/components/header.blade.php index 8f1ba9c..b9b3318 100644 --- a/resources/views/components/header.blade.php +++ b/resources/views/components/header.blade.php @@ -86,10 +86,10 @@
Usd -
@@ -118,8 +118,8 @@ {{--
  • Blog Details
  • --}} - - {{-- @if (session()->has('id')) + {{-- + @if (session()->has('id'))
  • Logout
  • @else
  • Login
  • diff --git a/routes/web.php b/routes/web.php index 71867b9..c5175ed 100644 --- a/routes/web.php +++ b/routes/web.php @@ -12,7 +12,7 @@ Route::get('/blogDetails', [MainController::class, 'blogDetails']); Route::get('/cart', [MainController::class, 'cart']); Route::post('/addToCart', [MainController::class, 'addToCart']); Route::get('/deleteCartItem/{id}', [MainController::class, 'deleteCartItem']); -Route::post('updateCartItem/{id}', [MainController::class, 'updateCartItem'])->name("cart.update"); +Route::post('/updateCartItem/{id}', [MainController::class, 'updateCartItem'])->name("cart.update"); Route::get('/checkout', [MainController::class, 'checkout']);
    @@ -91,13 +106,19 @@ - +
    Nrs. {{ $item->price * $item->quantity }}
    + +