From 71944f32459ddabdfd41757f2dacecf15d34898f Mon Sep 17 00:00:00 2001 From: UronShrestha Date: Fri, 5 Jul 2024 12:40:14 +0545 Subject: [PATCH] Read Cart Items with JOINS --- app/Http/Controllers/MainController.php | 62 ++++---- .../2024_07_04_080725_create_carts_table.php | 6 +- public/css/style.css | 10 +- resources/views/cart.blade.php | 133 +++++++----------- resources/views/components/header.blade.php | 22 ++- resources/views/index.blade.php | 1 + routes/web.php | 1 + 7 files changed, 117 insertions(+), 118 deletions(-) diff --git a/app/Http/Controllers/MainController.php b/app/Http/Controllers/MainController.php index 698df09..3b533f1 100644 --- a/app/Http/Controllers/MainController.php +++ b/app/Http/Controllers/MainController.php @@ -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')) { diff --git a/database/migrations/2024_07_04_080725_create_carts_table.php b/database/migrations/2024_07_04_080725_create_carts_table.php index 834a010..5efc504 100644 --- a/database/migrations/2024_07_04_080725_create_carts_table.php +++ b/database/migrations/2024_07_04_080725_create_carts_table.php @@ -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(); }); diff --git a/public/css/style.css b/public/css/style.css index b6f44db..e758eb3 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -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 { diff --git a/resources/views/cart.blade.php b/resources/views/cart.blade.php index edb5214..a297639 100644 --- a/resources/views/cart.blade.php +++ b/resources/views/cart.blade.php @@ -8,8 +8,8 @@ @@ -34,88 +34,63 @@ + + + {{-- + @foreach ($cartItems as $item) + + +
+ +
+ + + +
+
+ {{ $item->name }} +
+
+ Nrs {{ $item->price }} +
+
+
+ + + +
+ +
Nrs. {{ $item->price * $item->quantity }} + + + @endforeach + --}} + - - -
- -
-
-
T-shirt Contrast Pocket
-
$98.49
-
- - -
-
- + @foreach ($cartItems as $item) + + +
+
-
- - $ 30.00 - - - - -
- -
-
-
Diagonal Textured Cap
-
$98.49
-
- - -
-
- + + +
+
{{ $item->name }}
+
Nrs {{ $item->price }}
-
- - $ 32.50 - - - - -
- -
-
-
Basic Flowing Scarf
-
$98.49
-
- - -
-
- +
+
-
- - $ 47.00 - - - - -
- -
-
-
Basic Flowing Scarf
-
$98.49
-
- - -
-
- -
-
- - $ 30.00 - - + +
Nrs. {{ $item->price * $item->quantity }} + + + @endforeach +
diff --git a/resources/views/components/header.blade.php b/resources/views/components/header.blade.php index 9c00c95..8f1ba9c 100644 --- a/resources/views/components/header.blade.php +++ b/resources/views/components/header.blade.php @@ -74,8 +74,15 @@
Usd @@ -105,19 +112,19 @@
  • Pages
  • - @if (session()->has('id')) + {{-- @if (session()->has('id'))
  • Logout
  • @else
  • Login
  • Register
  • - @endif + @endif --}} @@ -127,7 +134,8 @@ - + 0
    Rs 0.00
    diff --git a/resources/views/index.blade.php b/resources/views/index.blade.php index 287e87d..7d6d92f 100644 --- a/resources/views/index.blade.php +++ b/resources/views/index.blade.php @@ -120,6 +120,7 @@
    {{ $item->name }}
    + + Add To Cart
    diff --git a/routes/web.php b/routes/web.php index 6cd5d7b..ddfca93 100644 --- a/routes/web.php +++ b/routes/web.php @@ -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']);