我有一个购物车使用,我可以将产品添加到购物车。现在我想从我的购物车中删除和更新产品。 在下面的代码中,我展示了我的控制器、cart.php、路线和购物车页面。现在我可以将产品添加到购物车,但我在更新和删除购物车中的项目时遇到了一些问题。 可以帮我吗..谢谢
这是我得到的错误:
Call to undefined method App\Cart::remove()
不是:我正在为此购物车使用会话
这是我的 Cart.php
<?php
namespace App;
/**
*
*/
class Cart
{
public $items = NULL;
public $totalQty = 0;
public $totalPrice = 0;
public function __construct($oldCart)
{
if ($oldCart) {
$this->items = $oldCart->items;
$this->totalQty = $oldCart->totalQty;
$this->totalPrice = $oldCart->totalPrice;
}
}
public function add($item, $id){
$storedItem = ['qty' => 0, 'price' => $item->price, 'item' => $item];
if ($this->items) {
if (array_key_exists($id, $this->items)) {
$storedItem = $this->items[$id];
}
}
$storedItem['qty']++;
$storedItem['price'] = $item->price * $storedItem['qty'];
$this->items[$id] = $storedItem;
$this->totalQty++;
$this->totalPrice += $item->price;
}
}
这是我的 productController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Menu;
use App\Models\Category;
use App\Models\AboutUs;
use App\Models\Slider;
use App\Models\Product;
use App\Models\Info;
use App\Models\Banner;
use Session;
use App\Cart;
public function getAddToCart(Request $request, $id){
$product = Product::find($id);
$oldCart = Session::has('cart') ? Session::get('cart') : NULL;
$cart = new Cart($oldCart);
$cart->add($product, $product->id);
$request->session()->put('cart', $cart);
return redirect()->route('products')->with('success', 'Produit ajouté au panier avec succès.');
}
public function getCart($slug=NULL){
if (!Session::has('cart')) {
return view('shopCart');
}
$product = Product::where('slug', $slug)->first();
$menus = Menu::orderBy('id')->get();
$categoryall = Category::select('*')
->where('status', 1)
->get();
$infos = Info::where('status', 1)->first();
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
return view('shopCart', [
'products' => $cart->items,
'totalPrice' => $cart->totalPrice,
'product' => $product,
'menus' => $menus,
'categoryall' => $categoryall,
'infos' => $infos,
]);
}
public function destroy($id)
{
Cart::remove($id);
return back()->with('success', 'Produit Supprimé du Panier');
}
这是我的路线
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\CartController;
Route::post('add-to-cart/{id}', [HomeController::class, 'getAddToCart'])
->name('addToCart');
Route::get('shoppingCart', [HomeController::class, 'getCart'])
->name('shoppingCart');
Route::get('/cart/productDelete', [HomeController::class, 'destroy'])
->name('cart.product.delete');
这是我的 shopCart.blade.php
@if (Session::has('cart'))
<section class="cart-page pt-10">
<div class="container-fluid custom-container">
<div class="row">
<div class="col-xl-8">
<div class="shopping-cart mt-25">
<h4 class="allup-title">Shopping Cart</h4>
<div class="shopping-cart-table table-responsive">
<table class="table">
<thead>
<tr>
<th class="image">Image</th>
<th class="product">Product</th>
<th class="price">Price</th>
<th class="quantity">Quantity</th>
<th class="total">Total</th>
<th class="delete">Delete</th>
</tr>
</thead>
<tbody>
@foreach ($products as $product)
<tr>
<td>
<div class="product-image">
<img width="50" src="{{ url('frontend/assets/images/product/'.$product['item']['file']) }}" alt="cart">
</div>
</td>
<td>
<div class="product-title">
<h4 class="title">
<a href="single-product.html">{{$product['item']['name']}}</a></h4>
</div>
</td>
<td>
<div class="product-price">
<span class="price">{{$product['price']}} €</span>
</div>
</td>
<td>
<div class="product-quantity">
<div class="quantity mt-15 d-flex">
<button type="button" class="sub"><i class="fal fa-minus"></i></button>
<input type="text" name="qty" value="{{$product['qty']}}"/>
<button type="button" class="add">
<i class="fal fa-plus"></i>
</button>
</div>
</div>
</td>
<td>
<div class="product-total">
<span class="total-amount">€18.90</span>
</div>
</td>
<td>
<div class="product-delete">
<a href="">
<button type="submit"><i class="fal fa-trash-alt"></i></button>
</a>
</div>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<div class="cart-btn text-right">
<form method="POST" action="">
@csrf
<button class="main-btn" type="submit">Update Cart</button>
</form>
</div>
</div>
</div>
<div class="col-xl-4">
<div class="row">
<div class="col-xl-12 col-md-6">
<div class="cart-coupon mt-30">
<h5 class="title">Discount Coupon Code</h5>
<form action="#">
<div class="single-form coupon-form d-flex flex-wrap">
<input type="text" placeholder="Coupon Code">
<button class="main-btn">Apply Coupon</button>
</div>
</form>
</div>
</div>
<div class="col-xl-12 col-md-6">
<div class="cart-total mt-30">
<div class="sub-total">
<div class="single-total">
<span class="cart-value">Subtotal</span>
<span class="cart-amount">€18.90</span>
</div>
<div class="single-total">
<span class="cart-value">Shipping Cost</span>
<span class="cart-amount">€3.90</span>
</div>
</div>
<div class="total">
<div class="single-total">
<span class="cart-value">Total (Tax.)</span>
<span class="cart-amount">{{$totalPrice}} FCFA</span>
</div>
<div class="single-total">
<span class="cart-value">Taxes</span>
<span class="cart-amount">€0.00</span>
</div>
</div>
<div class="cart-total-btn text-right">
<a class="main-btn" href="#">Proceed to Checkout</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<!--====== Cart Part Ends ======-->
<!--====== Brand Part Start ======-->
<div class="brand-area pt-80">
<div class="container-fluid custom-container">
<div class="row brand-active">
<div class="col-lg-2">
<div class="single-brand">
<a href="#">
<img src="assets/images/brand/brand-1.jpg" alt="brand">
</a>
</div> <!-- single brand -->
</div>
<div class="col-lg-2">
<div class="single-brand">
<a href="#">
<img src="assets/images/brand/brand-2.jpg" alt="brand">
</a>
</div> <!-- single brand -->
</div>
<div class="col-lg-2">
<div class="single-brand">
<a href="#">
<img src="assets/images/brand/brand-3.jpg" alt="brand">
</a>
</div> <!-- single brand -->
</div>
<div class="col-lg-2">
<div class="single-brand">
<a href="#">
<img src="assets/images/brand/brand-4.jpg" alt="brand">
</a>
</div> <!-- single brand -->
</div>
<div class="col-lg-2">
<div class="single-brand">
<a href="#">
<img src="assets/images/brand/brand-5.jpg" alt="brand">
</a>
</div> <!-- single brand -->
</div>
<div class="col-lg-2">
<div class="single-brand">
<a href="#">
<img src="assets/images/brand/brand-6.jpg" alt="brand">
</a>
</div> <!-- single brand -->
</div>
<div class="col-lg-2">
<div class="single-brand">
<a href="#">
<img src="assets/images/brand/brand-4.jpg" alt="brand">
</a>
</div> <!-- single brand -->
</div>
</div> <!-- row -->
</div> <!-- container -->
</div>
<!--====== Brand Part Ends ======-->
<!--====== Features Banner Part Start ======-->
<section class="features-banner-area pt-80 pb-100">
<div class="container-fluid custom-container">
<div class="features-banner-wrapper d-flex flex-wrap">
<div class="single-features-banner d-flex">
<div class="banner-icon">
<img src="assets/images/banner-icon/icon1.png" alt="Icon">
</div>
<div class="banner-content media-body">
<h3 class="banner-title">Free Shipping</h3>
<p>Free shipping on all US order</p>
</div>
</div> <!-- single features banner -->
<div class="single-features-banner d-flex">
<div class="banner-icon">
<img src="assets/images/banner-icon/icon2.png" alt="Icon">
</div>
<div class="banner-content media-body">
<h3 class="banner-title">Support 24/7</h3>
<p>Contact us 24 hours a day</p>
</div>
</div> <!-- single features banner -->
<div class="single-features-banner d-flex">
<div class="banner-icon">
<img src="assets/images/banner-icon/icon3.png" alt="Icon">
</div>
<div class="banner-content media-body">
<h3 class="banner-title">100% Money Back</h3>
<p>You have 30 days to Return</p>
</div>
</div> <!-- single features banner -->
<div class="single-features-banner d-flex">
<div class="banner-icon">
<img src="assets/images/banner-icon/icon4.png" alt="Icon">
</div>
<div class="banner-content media-body">
<h3 class="banner-title">90 Days Return</h3>
<p>If goods have problems</p>
</div>
</div> <!-- single features banner -->
<div class="single-features-banner d-flex">
<div class="banner-icon">
<img src="assets/images/banner-icon/icon5.png" alt="Icon">
</div>
<div class="banner-content media-body">
<h3 class="banner-title">Payment Secure</h3>
<p>We ensure secure payment</p>
</div>
</div> <!-- single features banner -->
</div> <!-- features banner wrapper -->
</div> <!-- container -->
</section>
@else
{{__('Votre Panier est Vide')}}
@endif