Read Cart Items with JOINS

This commit is contained in:
UronShrestha 2024-07-05 12:40:14 +05:45
parent 080094d2dd
commit 71944f3245
7 changed files with 117 additions and 118 deletions

View File

@ -3,6 +3,9 @@
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use App\Models\Cart;
use App\Models\Products;
use Illuminate\Http\Request;
@ -31,35 +34,7 @@ class MainController extends Controller
return view('register');
}
// public function registerUser(Request $data)
// {
// // Validate the input data
// $data->validate([
// 'name' => 'required|string|max:255',
// 'email' => 'required|string|email|max:255|unique:users',
// 'password' => 'required|string|min:8|confirmed',
// 'file' => 'required|file|mimes:jpg,png,jpeg|max:2048',
// ]);
// // Check if the email already exists
// if (User::where('email', $data->input('email'))->exists()) {
// return redirect('register')->with('error', 'Email already exists!');
// }
// // Create a new user
// $newUser = new User();
// $newUser->name = $data->input('name');
// $newUser->email = $data->input('email');
// $newUser->password = Hash::make($data->input('password')); // Hashing the password
// $newUser->picture = $data->file('file')->getClientOriginalName();
// $data->file('file')->move('uploads/profiles/', $newUser->picture);
// $newUser->type = "Customer";
// if ($newUser->save()) {
// return redirect('login')->with('success', 'Account created successfully!');
// }
// return view('register')->with('error', 'Account creation failed. Please try again.');
// }
public function registerUser(Request $data)
{
@ -139,6 +114,37 @@ class MainController extends Controller
return view('blogDetails');
}
// public function cart()
// {
// $cartItems = DB::table('products')
// ->join('carts', 'carts.productsId', 'product.id')
// ->select('products.name', 'products.price', 'products.picture', 'products.quantity as pQuantity', 'carts.*')
// ->where('carts.customerId', session()->get('id'))
// ->get();
// dd($cartItems);
// return view('cart', compact('cartItems'));
// }
public function cart()
{
$cartItems = DB::table('products')
->join('carts', 'carts.productId', '=', 'products.id')
->select('products.name', 'products.price', 'products.picture', 'products.quantity as pQuantity', 'carts.*')
->where('carts.customerId', session()->get('id'))
->get();
//dd($cartItems);
return view('cart', compact('cartItems'));
}
public function addToCart(Request $data)
{
if (session()->has('id')) {

View File

@ -13,9 +13,9 @@ return new class extends Migration
{
Schema::create('carts', function (Blueprint $table) {
$table->id();
$table->integer('productId')->default(0);
$table->integer('customerId')->default(0);
$table->integer('quantity')->default(0);
$table->integer('productId')->nullable();
$table->integer('customerId')->nullable();
$table->integer('quantity')->nullable();
$table->timestamps();
});

View File

@ -2329,12 +2329,20 @@ ol {
}
.shopping__cart__table table tbody tr td.product__cart__item {
width: 400px;
width: 300px;
}
.shopping__cart__table table tbody tr td.product__cart__item .product__cart__item__pic {
float: left;
margin-right: 30px;
/* height: 260px; */
/* float: left;
margin-right: 30px;
height: 260px;
width: 100%;
object-fit: contain;
object-position: center; */
}
.shopping__cart__table table tbody tr td.product__cart__item .product__cart__item__text {

View File

@ -8,8 +8,8 @@
<div class="breadcrumb__text">
<h4>Shopping Cart</h4>
<div class="breadcrumb__links">
<a href="./index.html">Home</a>
<a href="./shop.html">Shop</a>
<a href="{{ url::to('/') }}">Home</a>
<a href="{{ url::to('shop') }}">Shop</a>
<span>Shopping Cart</span>
</div>
</div>
@ -34,88 +34,63 @@
<th></th>
</tr>
</thead>
{{-- <tbody>
@foreach ($cartItems as $item)
<tr>
<td class="product__cart__item">
<div class="product__cart__item__pic">
<img src="{{ asset('uploads/products/' . $item->picture) }}" alt="">
</div>
</td>
<td class="quantity__item">
<div class="product__cart__item__text">
<h6>
{{ $item->name }}
</h6>
<h5>
<b>Nrs {{ $item->price }}</b>
</h5>
</div>
<div class="quantity">
<input type="number" class="form-control" name="quantity" min="1"
max="{{ $item->pQuantity }}" value="{{ $item->quantity }} ">
</div>
</td>
<td class="cart__price"> <br> Nrs. {{ $item->price * $item->quantity }} </td>
<td class="cart__close"><i class="fa fa-close"></i></td>
</tr>
@endforeach
</tbody> --}}
<tbody>
@foreach ($cartItems as $item)
<tr>
<td class="product__cart__item">
<div class="product__cart__item__pic">
<img src="img/shopping-cart/cart-1.jpg" alt="">
</div>
<div class="product__cart__item__text">
<h6>T-shirt Contrast Pocket</h6>
<h5>$98.49</h5>
<img src="{{ asset('uploads/products/' . $item->picture) }}" alt="">
</div>
</td>
<td class="quantity__item">
<div class="quantity">
<div class="pro-qty-2">
<input type="text" value="1">
</div>
</div>
</td>
<td class="cart__price">$ 30.00</td>
<td class="cart__close"><i class="fa fa-close"></i></td>
</tr>
<tr>
<td class="product__cart__item">
<div class="product__cart__item__pic">
<img src="img/shopping-cart/cart-2.jpg" alt="">
</div>
<div class="product__cart__item__text">
<h6>Diagonal Textured Cap</h6>
<h5>$98.49</h5>
<h6>{{ $item->name }}</h6>
<h5><b>Nrs {{ $item->price }}</b></h5>
</div>
</td>
<td class="quantity__item">
<div class="quantity">
<div class="pro-qty-2">
<input type="text" value="1">
</div>
<input type="number" class="form-control" name="quantity" min="1"
max="{{ $item->pQuantity }}" value="{{ $item->quantity }}">
</div>
</td>
<td class="cart__price">$ 32.50</td>
<td class="cart__close"><i class="fa fa-close"></i></td>
</tr>
<tr>
<td class="product__cart__item">
<div class="product__cart__item__pic">
<img src="img/shopping-cart/cart-3.jpg" alt="">
</div>
<div class="product__cart__item__text">
<h6>Basic Flowing Scarf</h6>
<h5>$98.49</h5>
</div>
</td>
<td class="quantity__item">
<div class="quantity">
<div class="pro-qty-2">
<input type="text" value="1">
</div>
</div>
</td>
<td class="cart__price">$ 47.00</td>
<td class="cart__close"><i class="fa fa-close"></i></td>
</tr>
<tr>
<td class="product__cart__item">
<div class="product__cart__item__pic">
<img src="img/shopping-cart/cart-4.jpg" alt="">
</div>
<div class="product__cart__item__text">
<h6>Basic Flowing Scarf</h6>
<h5>$98.49</h5>
</div>
</td>
<td class="quantity__item">
<div class="quantity">
<div class="pro-qty-2">
<input type="text" value="1">
</div>
</div>
</td>
<td class="cart__price">$ 30.00</td>
<td class="cart__price"><br> Nrs. {{ $item->price * $item->quantity }}</td>
<td class="cart__close"><i class="fa fa-close"></i></td>
</tr>
@endforeach
</tbody>
</table>
</div>
<div class="row">

View File

@ -74,8 +74,15 @@
<div class="col-lg-6 col-md-5">
<div class="header__top__right">
<div class="header__top__links">
<a href="#">Sign in</a>
<a href="#">FAQs</a>
@if (session()->has('id'))
<a href="{{ URL::to('/logout') }}">Logout</a>
@else
<a href="{{ URL::to('/login') }}">Login</a>
<a href="{{ URL::to('/register') }}">Register</a>
@endif
{{-- <a href="{{ URL::to('/register') }}">Sign in</a>
<a href="{{ URL::to('/login') }}">Login</a> --}}
</div>
<div class="header__top__hover">
<span>Usd <i class="arrow_carrot-down"></i></span>
@ -105,19 +112,19 @@
<li><a href="#">Pages</a>
<ul class="dropdown">
<li><a href="{{ URL::to('about') }}">About Us</a></li>
<li><a href="{{ URL::to('singleProduct') }}">Single Product</a></li>
{{-- <li><a href="{{ URL::to('singleProduct') }}">Single Product</a></li> --}}
<li><a href="{{ URL::to('cart') }}">Shopping Cart</a></li>
<li><a href="{{ URL::to('checkout') }}">Check Out</a></li>
<li><a href="{{ URL::to('blogDetails') }}">Blog Details</a></li>
{{-- <li><a href="{{ URL::to('blogDetails') }}">Blog Details</a></li> --}}
</ul>
</li>
@if (session()->has('id'))
{{-- @if (session()->has('id'))
<li><a href="{{ URL::to('/logout') }}">Logout</a></li>
@else
<li><a href="{{ URL::to('/login') }}">Login</a></li>
<li><a href="{{ URL::to('/register') }}">Register</a></li>
@endif
@endif --}}
</ul>
</nav>
@ -127,7 +134,8 @@
<a href="#" class="search-switch"><img src="{{ URL::asset('img/icon/search.png') }}"
alt=""></a>
<a href="#"><img src="{{ URL::asset('img/icon/heart.png') }}" alt=""></a>
<a href="#"><img src="{{ URL::asset('img/icon/cart.png') }}" alt="">
<a href="{{ URL::to('cart') }}"><img src="{{ URL::asset('img/icon/cart.png') }}"
alt="">
<span>0</span></a>
<div class="price">Rs 0.00</div>
</div>

View File

@ -120,6 +120,7 @@
</div>
<div class="product__item__text">
<h6>{{ $item->name }}</h6>
<a href="#" class="add-cart">+ Add To Cart</a>
<div class="rating">
<i class="fa fa-star-o"></i>

View File

@ -9,6 +9,7 @@ Route::get('/about', [MainController::class, 'about']);
Route::get('/blog', [MainController::class, 'blog']);
Route::get('/blogDetails', [MainController::class, 'blogDetails']);
Route::get('/cart', [MainController::class, 'cart']);
Route::post('/addToCart', [MainController::class, 'addToCart']);
Route::get('/checkout', [MainController::class, 'checkout']);