From 080094d2dd3950e3c613e0ab6b6209aef85a6273 Mon Sep 17 00:00:00 2001 From: UronShrestha Date: Thu, 4 Jul 2024 17:52:47 +0545 Subject: [PATCH] Laravel Shopping Cart Tutorial| Add to Cart --- app/Http/Controllers/MainController.php | 15 +- app/Models/Cart.php | 11 + .../2024_07_04_080725_create_carts_table.php | 31 ++ resources/views/singleProduct.blade.php | 380 +++++++++--------- routes/web.php | 4 +- 5 files changed, 254 insertions(+), 187 deletions(-) create mode 100644 app/Models/Cart.php create mode 100644 database/migrations/2024_07_04_080725_create_carts_table.php diff --git a/app/Http/Controllers/MainController.php b/app/Http/Controllers/MainController.php index 7547001..698df09 100644 --- a/app/Http/Controllers/MainController.php +++ b/app/Http/Controllers/MainController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\Models\User; +use App\Models\Cart; use App\Models\Products; use Illuminate\Http\Request; use Illuminate\Support\Facades\Hash; @@ -138,9 +139,19 @@ class MainController extends Controller return view('blogDetails'); } - public function cart() + public function addToCart(Request $data) { - return view('cart'); + if (session()->has('id')) { + $item = new Cart(); + $item->quantity = $data->input('quantity'); + $item->productId = $data->input('id'); + $item->customerId = session()->get('id'); + + $item->save(); + return redirect()->back()->with('success', 'Item added to cart successfully!'); + } else { + return redirect('/login')->with('error', 'Please login to add item to cart!'); + } } public function checkout() diff --git a/app/Models/Cart.php b/app/Models/Cart.php new file mode 100644 index 0000000..c18a8ac --- /dev/null +++ b/app/Models/Cart.php @@ -0,0 +1,11 @@ +id(); + $table->integer('productId')->default(0); + $table->integer('customerId')->default(0); + $table->integer('quantity')->default(0); + + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('carts'); + } +}; diff --git a/resources/views/singleProduct.blade.php b/resources/views/singleProduct.blade.php index 91aa26d..0e8ecba 100644 --- a/resources/views/singleProduct.blade.php +++ b/resources/views/singleProduct.blade.php @@ -61,12 +61,12 @@
- +
- +
@@ -76,194 +76,203 @@
-
-
-
-
-
-

{{ $products->name }}

-
- - - - - - - 5 Reviews -
-

NRs. {{ $products->price }} 4000.00

-

{{ $products->description }}

-
-
- Size: - - - - +
+ @csrf + +
+
+
+
+
+

{{ $products->name }}

+
+ + + + + + - 5 Reviews
-
- Color: - - - - - -
-
-
-
-
- +

NRs. {{ $products->price }} 4000.00

+

{{ $products->description }}

+
+
+ Size: + + + + +
+
+ Color: + + + + +
- add to cart -
- -
-
Guaranteed Safe Checkout
- -
    -
  • SKU: 3812912
  • -
  • Categories: Clothes
  • -
  • Tag: Clothes, Skin, Body
  • -
+
+
+ + + +
+ + +
+ +
+
Guaranteed Safe Checkout
+ +
    +
  • SKU: 3812912
  • +
  • Categories: Clothes
  • +
  • Tag: Clothes, Skin, Body
  • +
+
-
-
-
-
- -
-
-
-

Nam tempus turpis at metus scelerisque placerat nulla deumantos - solicitud felis. Pellentesque diam dolor, elementum etos lobortis des mollis - ut risus. Sedcus faucibus an sullamcorper mattis drostique des commodo - pharetras loremos.

-
-
Products Infomation
-

A Pocket PC is a handheld computer, which features many of the same - capabilities as a modern PC. These handy little devices allow - individuals to retrieve and store e-mail messages, create a contact - file, coordinate appointments, surf the internet, exchange text messages - and more. Every product that is labeled as a Pocket PC must be - accompanied with specific software to operate the unit and must feature - a touchscreen and touchpad.

-

As is the case with any new technology product, the cost of a Pocket PC - was substantial during it’s early release. For approximately $700.00, - consumers could purchase one of top-of-the-line Pocket PCs in 2003. - These days, customers are finding that prices have become much more - reasonable now that the newness is wearing off. For approximately - $350.00, a new Pocket PC can now be purchased.

-
-
-
Material used
-

Polyester is deemed lower quality due to its none natural quality’s. Made - from synthetic materials, not natural like wool. Polyester suits become - creased easily and are known for not being breathable. Polyester suits - tend to have a shine to them compared to wool and cotton suits, this can - make the suit look cheap. The texture of velvet is luxurious and - breathable. Velvet is a great choice for dinner party jacket and can be - worn all year round.

+
+
+
+ +
+
+
+

Nam tempus turpis at metus scelerisque placerat nulla + deumantos + solicitud felis. Pellentesque diam dolor, elementum etos lobortis des mollis + ut risus. Sedcus faucibus an sullamcorper mattis drostique des commodo + pharetras loremos.

+
+
Products Infomation
+

A Pocket PC is a handheld computer, which features many of the same + capabilities as a modern PC. These handy little devices allow + individuals to retrieve and store e-mail messages, create a contact + file, coordinate appointments, surf the internet, exchange text messages + and more. Every product that is labeled as a Pocket PC must be + accompanied with specific software to operate the unit and must feature + a touchscreen and touchpad.

+

As is the case with any new technology product, the cost of a Pocket PC + was substantial during it’s early release. For approximately $700.00, + consumers could purchase one of top-of-the-line Pocket PCs in 2003. + These days, customers are finding that prices have become much more + reasonable now that the newness is wearing off. For approximately + $350.00, a new Pocket PC can now be purchased.

+
+
+
Material used
+

Polyester is deemed lower quality due to its none natural quality’s. Made + from synthetic materials, not natural like wool. Polyester suits become + creased easily and are known for not being breathable. Polyester suits + tend to have a shine to them compared to wool and cotton suits, this can + make the suit look cheap. The texture of velvet is luxurious and + breathable. Velvet is a great choice for dinner party jacket and can be + worn all year round.

+
-
-
-
-
-
Products Infomation
-

A Pocket PC is a handheld computer, which features many of the same - capabilities as a modern PC. These handy little devices allow - individuals to retrieve and store e-mail messages, create a contact - file, coordinate appointments, surf the internet, exchange text messages - and more. Every product that is labeled as a Pocket PC must be - accompanied with specific software to operate the unit and must feature - a touchscreen and touchpad.

-

As is the case with any new technology product, the cost of a Pocket PC - was substantial during it’s early release. For approximately $700.00, - consumers could purchase one of top-of-the-line Pocket PCs in 2003. - These days, customers are finding that prices have become much more - reasonable now that the newness is wearing off. For approximately - $350.00, a new Pocket PC can now be purchased.

-
-
-
Material used
-

Polyester is deemed lower quality due to its none natural quality’s. Made - from synthetic materials, not natural like wool. Polyester suits become - creased easily and are known for not being breathable. Polyester suits - tend to have a shine to them compared to wool and cotton suits, this can - make the suit look cheap. The texture of velvet is luxurious and - breathable. Velvet is a great choice for dinner party jacket and can be - worn all year round.

+
+
+
+
Products Infomation
+

A Pocket PC is a handheld computer, which features many of the same + capabilities as a modern PC. These handy little devices allow + individuals to retrieve and store e-mail messages, create a contact + file, coordinate appointments, surf the internet, exchange text messages + and more. Every product that is labeled as a Pocket PC must be + accompanied with specific software to operate the unit and must feature + a touchscreen and touchpad.

+

As is the case with any new technology product, the cost of a Pocket PC + was substantial during it’s early release. For approximately $700.00, + consumers could purchase one of top-of-the-line Pocket PCs in 2003. + These days, customers are finding that prices have become much more + reasonable now that the newness is wearing off. For approximately + $350.00, a new Pocket PC can now be purchased.

+
+
+
Material used
+

Polyester is deemed lower quality due to its none natural quality’s. Made + from synthetic materials, not natural like wool. Polyester suits become + creased easily and are known for not being breathable. Polyester suits + tend to have a shine to them compared to wool and cotton suits, this can + make the suit look cheap. The texture of velvet is luxurious and + breathable. Velvet is a great choice for dinner party jacket and can be + worn all year round.

+
-
-
-
-

Nam tempus turpis at metus scelerisque placerat nulla deumantos - solicitud felis. Pellentesque diam dolor, elementum etos lobortis des mollis - ut risus. Sedcus faucibus an sullamcorper mattis drostique des commodo - pharetras loremos.

-
-
Products Infomation
-

A Pocket PC is a handheld computer, which features many of the same - capabilities as a modern PC. These handy little devices allow - individuals to retrieve and store e-mail messages, create a contact - file, coordinate appointments, surf the internet, exchange text messages - and more. Every product that is labeled as a Pocket PC must be - accompanied with specific software to operate the unit and must feature - a touchscreen and touchpad.

-

As is the case with any new technology product, the cost of a Pocket PC - was substantial during it’s early release. For approximately $700.00, - consumers could purchase one of top-of-the-line Pocket PCs in 2003. - These days, customers are finding that prices have become much more - reasonable now that the newness is wearing off. For approximately - $350.00, a new Pocket PC can now be purchased.

-
-
-
Material used
-

Polyester is deemed lower quality due to its none natural quality’s. Made - from synthetic materials, not natural like wool. Polyester suits become - creased easily and are known for not being breathable. Polyester suits - tend to have a shine to them compared to wool and cotton suits, this can - make the suit look cheap. The texture of velvet is luxurious and - breathable. Velvet is a great choice for dinner party jacket and can be - worn all year round.

+
+
+

Nam tempus turpis at metus scelerisque placerat nulla + deumantos + solicitud felis. Pellentesque diam dolor, elementum etos lobortis des mollis + ut risus. Sedcus faucibus an sullamcorper mattis drostique des commodo + pharetras loremos.

+
+
Products Infomation
+

A Pocket PC is a handheld computer, which features many of the same + capabilities as a modern PC. These handy little devices allow + individuals to retrieve and store e-mail messages, create a contact + file, coordinate appointments, surf the internet, exchange text messages + and more. Every product that is labeled as a Pocket PC must be + accompanied with specific software to operate the unit and must feature + a touchscreen and touchpad.

+

As is the case with any new technology product, the cost of a Pocket PC + was substantial during it’s early release. For approximately $700.00, + consumers could purchase one of top-of-the-line Pocket PCs in 2003. + These days, customers are finding that prices have become much more + reasonable now that the newness is wearing off. For approximately + $350.00, a new Pocket PC can now be purchased.

+
+
+
Material used
+

Polyester is deemed lower quality due to its none natural quality’s. Made + from synthetic materials, not natural like wool. Polyester suits become + creased easily and are known for not being breathable. Polyester suits + tend to have a shine to them compared to wool and cotton suits, this can + make the suit look cheap. The texture of velvet is luxurious and + breathable. Velvet is a great choice for dinner party jacket and can be + worn all year round.

+
@@ -272,7 +281,7 @@
-
+ @@ -287,7 +296,8 @@
-
+
New