Place Order

This commit is contained in:
UronShrestha 2024-07-07 13:06:31 +05:45
parent 7b2a70ca1d
commit 49d6315cc4
9 changed files with 189 additions and 27 deletions

View File

@ -8,6 +8,8 @@ use Illuminate\Support\Facades\DB;
use App\Models\Cart; use App\Models\Cart;
use App\Models\Products; use App\Models\Products;
use App\Models\Order;
use App\Models\OrderItem;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Hash;
@ -163,25 +165,14 @@ class MainController extends Controller
return redirect()->back()->with('success', 'Item deleted from cart successfully!'); 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()); // dd($request->all());
if (session()->has('id')) { if (session()->has('id')) {
$item = Cart::find($id); $item = Cart::find($id);
if ($item) { if ($item) {
$item->quantity = $request->input('quantity'); $item->quantity = $data->input('quantity');
$item->save(); $item->save();
return redirect()->back()->with('success', 'Item updated successfully!'); return redirect()->back()->with('success', 'Item updated successfully!');
} else { } 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'); return view('checkout');
} }

11
app/Models/Order.php Normal file
View File

@ -0,0 +1,11 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
use HasFactory;
}

11
app/Models/OrderItem.php Normal file
View File

@ -0,0 +1,11 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class OrderItem extends Model
{
use HasFactory;
}

View File

@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('orders', function (Blueprint $table) {
$table->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');
}
};

View File

@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('order_items', function (Blueprint $table) {
$table->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');
}
};

View File

@ -27,11 +27,19 @@
<div class="shopping__cart__table"> <div class="shopping__cart__table">
@if (session()->has('success')) @if (session()->has('success'))
<div class="alert alert-success"> <div id="success-message" class="alert alert-success">
{{ session()->get('success') }} {{ session()->get('success') }}
</div> </div>
@endif @endif
@if (session()->has('error'))
<div id="error-message" class="alert alert-danger">
{{ session()->get('error') }}
</div>
@endif
<table> <table>
<thead> <thead>
<tr> <tr>
@ -72,7 +80,14 @@
</tr> </tr>
@endforeach @endforeach
</tbody> --}} </tbody> --}}
<tbody> <tbody>
@php
$total = 0;
@endphp
@foreach ($cartItems as $item) @foreach ($cartItems as $item)
<tr> <tr>
<td class="product__cart__item"> <td class="product__cart__item">
@ -91,13 +106,19 @@
<input type="number" class="form-control" name="quantity" <input type="number" class="form-control" name="quantity"
min="1" max="{{ $item->pQuantity }}" min="1" max="{{ $item->pQuantity }}"
value="{{ $item->quantity }}"> value="{{ $item->quantity }}">
<button type="submit" class="btn btn-primary">Update</button> <button type="submit"
class="btn btn-primary mt-2 btn-block">Update</button>
</form> </form>
</div> </div>
</td> </td>
<td class="cart__price"><br> Nrs. {{ $item->price * $item->quantity }}</td> <td class="cart__price"><br> Nrs. {{ $item->price * $item->quantity }}</td>
<td class="cart__close"> <a href="{{ url::to('deleteCartItem/' . $item->id) }}">
<i class="fa fa-trash"></i></a>
</td>
</tr> </tr>
@php
$total += $item->price * $item->quantity;
@endphp
@endforeach @endforeach
</tbody> </tbody>
@ -129,10 +150,27 @@
<div class="cart__total"> <div class="cart__total">
<h6>Cart total</h6> <h6>Cart total</h6>
<ul> <ul>
<li>Subtotal <span>$ 169.50</span></li> <li>Subtotal <span>Nrs {{ $total }} </span></li>
<li>Total <span>$ 169.50</span></li> <li>Total <span>Nrs {{ $total }}</span></li>
</ul> </ul>
<a href="#" class="primary-btn">Proceed to checkout</a> <form action=" {{ url::to('checkout') }} ">
<input type="text" name="name" class="form-control mt-2" placeholder="Enter Name"
id="" required>
<input type="text" name="phone" class="form-control mt-2"
placeholder
="Enter Phone" id="" required>
<input type="text" name="address" class="form-control mt-2"
placeholder
="Enter Address" id="" required>
<input type="hidden" name="bill" class="form-control mt-2" value="{{ $total }}"
id="" required>
<input type="submit" name="checkout" class="primary-btn mt-2 btn-block"
value="Proceed to Checkout" id="" required>
</form>
</div> </div>
</div> </div>
</div> </div>

View File

@ -90,4 +90,23 @@
<script src={{ URL::asset('js/main.js') }}></script> <script src={{ URL::asset('js/main.js') }}></script>
</body> </body>
<script>
document.addEventListener("DOMContentLoaded", function() {
setTimeout(function() {
var successMessage = document.getElementById('success-message');
if (successMessage) {
successMessage.style.display = 'none';
}
}, 2000); // 3000 milliseconds = 3 seconds
setTimeout(function() {
var errorMessage = document.getElementById('error-message');
if (errorMessage) {
errorMessage.style.display = 'none';
}
}, 2000); // 2000 milliseconds = 3 seconds
});
</script>
</html> </html>

View File

@ -86,10 +86,10 @@
</div> </div>
<div class="header__top__hover"> <div class="header__top__hover">
<span>Usd <i class="arrow_carrot-down"></i></span> <span>Usd <i class="arrow_carrot-down"></i></span>
<ul> <ul class="dropdown">
<li>USD</li> <li> <a href="#">USD</a></li>
<li>EUR</li> <li> <a href="#">EUR</a></li>
<li>NPR</li> <li> <a href="#">NEP</a></li>
</ul> </ul>
</div> </div>
</div> </div>
@ -118,8 +118,8 @@
{{-- <li><a href="{{ URL::to('blogDetails') }}">Blog Details</a></li> --}} {{-- <li><a href="{{ URL::to('blogDetails') }}">Blog Details</a></li> --}}
</ul> </ul>
</li> </li>
{{--
{{-- @if (session()->has('id')) @if (session()->has('id'))
<li><a href="{{ URL::to('/logout') }}">Logout</a></li> <li><a href="{{ URL::to('/logout') }}">Logout</a></li>
@else @else
<li><a href="{{ URL::to('/login') }}">Login</a></li> <li><a href="{{ URL::to('/login') }}">Login</a></li>

View File

@ -12,7 +12,7 @@ Route::get('/blogDetails', [MainController::class, 'blogDetails']);
Route::get('/cart', [MainController::class, 'cart']); Route::get('/cart', [MainController::class, 'cart']);
Route::post('/addToCart', [MainController::class, 'addToCart']); Route::post('/addToCart', [MainController::class, 'addToCart']);
Route::get('/deleteCartItem/{id}', [MainController::class, 'deleteCartItem']); 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']); Route::get('/checkout', [MainController::class, 'checkout']);